0

How do I get a CSS property from the style attribute of an HTML element (and ignore the stylesheet/computed properties)?

E.g:

<div style="float:left"></div>

function getStyle(element, name) { ... }
getStyle(element, 'float') === 'left';
getStyle(element, 'font-weight') === null;

I don't mind either raw JS or jQuery.

1
  • Be careful about what you ask for. HTML attributes and DOM properties are different things, though HTML5 tries to make them reflect each other, they do not in all cases in all browsers. For a long time, Firefox treated attributes and properties as two separate things, IE treated them as essentially the same thing (probably why HTML5 tries to take a middle path, more or less). They are not consistent yet. Commented Nov 27, 2012 at 4:46

5 Answers 5

2

If you want pure JS, This example:

element.style.display = 'none';
Sign up to request clarification or add additional context in comments.

1 Comment

Seems to work, Ill do some more testing before I accept the answer.
2

Does this not work for you?

$('div').attr('style');

If you want to parse this further:

var getStyles = function(element) {

    // ensure element has style
    if (! element.attr('style')) {
        return {};
    }

    // init styles
    var styles = {}

    // parse style attr
    $.each(element.attr('style').split(/\s*;\s*/), function(i,style) {
        if (style.length) { // a style string ending in ; will cause an empty pair after splitting
            pair = style.split(/\s*:\s*/);
            styles[pair[0]] = pair[1];
        }
    });

    return styles;
};

A couple tests

// test an element with style attr
var element = $('<div style="float:left"></div>');
console.log(getStyles(element)); //=> {float: "left"}
console.log(getStyles(element).float); //=> left
console.log(getStyles(element).hello); //=> undefined

// test some element without style attr
var element2 = $('<p>hello</p>');
console.log(getStyles(element2).float); //=> undefined
console.log(getStyles(element2).hello); //=> undefined

​// a bit more complex
var element3 = $('<div style="float:left; background-color:red; color:#555; opacity: 0.5 !important;"></div>');
console.log(getStyles(element3)); //=> {float: "left", opacity: "0.5 !important", background-color: "red", color: "#555"} 
console.log(getStyles(element3).float); //=> left
console.log(getStyles(element3).opacity) //=> 0.5 !important;
console.log(getStyles(element3)["background-color"]); //=> red

See it working on jsFiddle

Comments

0

You can use

var floating = $('div').css('float');
$('div').css('float','left');

To access each styling parameters with jQuery.

2 Comments

This will return the style-sheet inherited value, and/or the computed value. Which is not what I am after.
Ok now I see the difference, sorry!
0

you can also access via prop() function

$('div').prop('style');

Comments

0

Here's a quick improvement on macek's code.

This function is in the format asked by Petah. Plus, this function breaks as soon as it finds a match, hence eliminating the need to scan the entire "style" attribute.

function getStyle(element, name) {

if (! element.attr('style')) {
    return {};
}
var result= "";

$.each(element.attr('style').split(/\s*;\s*/), function(i,style) {
    if (style.length) { 
        pair = style.split(/\s*:\s*/);
        if(pair[0] == name) {
            result = pair[1];
            return false;
        }
    }
});

return result;
};

Js Fiddle for examples on usage : http://jsfiddle.net/rxpMJ/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.