views:

46

answers:

3

Hi,

How to detect whether a DOM element is block or inline with javascript?

For example, is there a function/property which returns 'inline' for a '<a>' tag (or 'block' for a '<p>' tag)?

Thank you.

A: 

the information you need should be in the display property within style.

element.style.display

Of course this will vary between browsers I expect like everyhting in javascript.

Wardy
No it shouldn't. The information will only be there if it has been set explicitly with JS or the style attribute. It won't include cascaded style and it won't include browser defaults.
David Dorward
are you sure about that? I've had a value from that property even when I haven't set it. Fair eough though if i'm wrong i'm wrong.
Wardy
+4  A: 

You can go with getComputedStyle() and currentStyle to get the calculated styles for an element. This should do it:

function getDisplayType (element) 
{
    var cStyle = element.currentStyle || window.getComputedStyle(element, ""); 
    return cStyle.display;
}

To be a little clearer, computed styles contain values for every style property, even for those that don't have a style property set. Those values will be the default value so in the case of an unstyled <a> element, display will return inline:

function getElementDefaultDisplay(tag)
{
    var cStyle,
        t = document.createElement(tag);

    document.body.appendChild(t);
    cStyle = (t.currentStyle || window.getComputedStyle(t, "")).display; 
    document.body.removeChild(t);

    return cStyle;
}

Tested in latest Firefox, Chrome and IE7/IE8.

Results:

> getElementDefaultDisplay("a")
inline
> getElementDefaultDisplay("div")
block
Andy E
Some errors that prevent this working in Firefox (untested in other browsers): `window.getComputedStyle` takes a second parameter, which can be "", and you need to get the `display` property using `cStyle.getPropertyValue("display")`. Needs some refactoring to work with the IE branch.
Tim Down
@Tim Down: it works just fine in IE: http://jsfiddle.net/fadrS/ -- I'll look into the Firefox thing.
Andy E
@Tim Down: all Firefox needed was the empty string as a second parameter: http://jsfiddle.net/fadrS/1/
Andy E
Yes, my point was only about the `window.getComputedStyle` branch (therefore all other browsers).
Tim Down
So it does. I'm not sure whether any browser needs the `getPropertyValue()` call: no recent browser seems to, but all the references I've seen on the web use it. Btw, Safari 2 doesn't support `window.getComputedStyle`.
Tim Down
Apparently `null` is more standards-compliant as the second parameter to `window.getComputedStyle` rather than an empty string.
Tim Down
@Tim Down: It strikes me as odd that the function **requires** that the parameter is passed. It certainly doesn't make much sense given the general nature of JavaScript, passing a null value to the final argument of a function just seems incredibly pointless.
Andy E
@Tim Down: Also, [NetMarketShare](http://marketshare.hitslink.com/browser-market-share.aspx?qprid=2) don't even have Safari 2 listed (Safari 3.0 has 0.07%, and the lowest browser has 0.03%). I think it's safe to not worry about it too much.
Andy E
A: 

The traditional and rather ugly way of doing this is to consult a list of element names for block-level elements:

var blockLevelElements = [
    "address", "blockquote", "body", "center", "dir", "div",
    "dl", "fieldset", "form", "h1", "h2", "h3", "h4", "h5", "h6",
    "hr", "isindex", "menu", "noframes", "noscript", "ol", "p",
    "pre", "table", "ul", "dd", "dt", "frameset", "li", "tbody",
    "td", "tfoot", "th", "thead", "tr", "html"
];

function contains(arr, val) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === val) {
            return true;
        }
    }
    return false;
}

function isBlockLevel(el) {
    return contains(blockLevelElements, el.nodeName.toLowerCase());
}
Tim Down
would this not just retrieve the default value rather than the actual one?
Wardy
Yes, it would. This isn't as good a solution as Andy E's `currentStyle` / `window.getComputedStyle` approach, but it used to have the advantage that it would work in pretty much any browser. These days with better browsers it's probably safe to use Andy E's approach. I gave this answer only as a fallback option, and I've thought about deleting it.
Tim Down