views:

309

answers:

4

Howdy all. I'd like to know if it's possible to determine what inline styling has been attributed to an HTML element. I do not need to retrieve the value, but rather just detect if it's been set inline or not.

For instance, if the HTML were:

<div id="foo" style="width: 100px; height: 100px; background: green;"></div>

How can I determine that "width," "height," and "background" have been explicitly declared, inline?

As far as I can tell, the solution can work two ways. I can ask it if a specific attribute is set and it will tell me true or false, or it can tell me all attributes that have been set. Like so:

var obj = document.getElementById('foo');
obj.hasInlineStyle('width');  //returns true;
obj.hasInlineStyle('border'); //returns false;

//or

obj.getInlineStyles();   //returns array with values:
                       // 'width' 'height' and 'background'

I'm not interested in css attributes that are inherited via declarations in a stylesheet, only inline styles. One last thing, I have no control over the HTML source.

Thanks

+1  A: 

The style property of an HTML Element returns a style object which lists all the properties. Any that have a value (other than null or empty string) have been set on the inline style attribute.

AnthonyWJones
Awesome, thanks!
sam
+3  A: 

Updated to work with IE

You could try something like this

function hasInlineStyle(obj, style) {
    var attrs = obj.getAttribute('style');
    if(attrs === null) return false;
    if(typeof attrs == 'object') attrs = attrs.cssText;
    var styles = attrs.split(';');
    for(var x = 0; x < styles.length; x++) {
        var attr = styles[x].split(':')[0].replace(/^\s+|\s+$/g,"").toLowerCase();
        if(attr == style) {
            return true;
        }
    }
    return false;
}

So if you have an element like this:

<span id='foo' style='color: #000; line-height:20px;'></span>

That also has a stylesheet like this:

#foo { background-color: #fff; }

The function would return...

var foo = document.getElementById('foo');
alert(hasInlineStyle(foo,'color')); // true
alert(hasInlineStyle(foo,'background-color')); // false
Paolo Bergantino
I was just looking through jQuery... looks like they insert stylesheet values as well (unacceptable in this case). Nice solution, though :)
sam
It works without jQuery now.
Paolo Bergantino
And the .attr('style') doesn't include stylesheet declarations in jQuery, only those set through style="" in the document.
Paolo Bergantino
Wait... disregard that last comment
sam
One last thing.. the null case (where getAttribute('style') is null)
sam
Ah, yes. Fixed it.
Paolo Bergantino
Beautiful.. I really appreciate it. I'm not so great with RegEx, what's that RegEx do? Trim?And now the real challenge... weird css attributes like "border" and "border-left."
sam
Yeah, the regex trims. What about "border" and "border-left"? The function should still work for them.
Paolo Bergantino
Do you mean you want border-left to return true if border was specified?
Paolo Bergantino
This seems to cause trouble in IE... I think .getAttribute("style") returns an object rather than a string
sam
Yeah, border-left should return true if border is set... but I've found a way around this so no worries.
sam
Mm. I can't find an adequate equivalent for getAttribute in IE...
Paolo Bergantino
Ouch. Nightmare mode.
sam
Found it. Updating now.
Paolo Bergantino
Updated. Works ok.
Paolo Bergantino
Amazing work... thank you so much!
sam
One IE quirk that you need to be aware of... if the element has an inline style of "border: 1px solid #000;" IE will say it has "border-left" "border-right" "border-top" "border-bottom" but not "border"... however, getting it to recognize these things is trivial.
Paolo Bergantino
A: 

You may want to try doing something like:

var obj = document.getElementById("foo");
var obj_has_background = (obj.style.background != "");
var obj_has_width = (obj.style.width != "");
var obj_has_height = (obj.style.height != "");

Seems a bit long, but this is the best (and shortest) solution I could come up with.

hydrapheetz
A: 

Are you trying to check if a certain styling attribute exists, or just want a list of the possible attributes? If you already know the attribute, then you can just use

hasStyle(obj,'width');
function hasStyle(obj, style)
{
     switch(style)
         case 'width':
              return obj.style.width ? true : false;
         case 'background':
              return obj.style.background ? true : false;
}

You can use Paolo's function to generate an array of the styles.