13

I need to get list of elements all css attributes. How can I do that ?

5
  • Do you want to get all the css properties of an element? Commented Sep 24, 2009 at 11:38
  • 1
    Yes, I need to get all css attributes that my selected element has. Commented Sep 24, 2009 at 11:41
  • related: stackoverflow.com/questions/754607/… Commented Sep 24, 2009 at 11:42
  • Also related: stackoverflow.com/questions/1004475/… Commented Sep 24, 2009 at 11:58
  • Useful answer: document.defaultView.getComputedStyle(document.body, '') got from stackoverflow.com/questions/2614963/… Commented Aug 1, 2013 at 9:16

4 Answers 4

20

Copying the source from SO1004475 - jQuery CSS plugin that returns computed style of element to pseudo clone that element? - Please follow link and upvote there if you find it useful.

It seems ridiculous, but this is probably your best option - makes .css() with no arguments get an object with all this stuff set.

jQuery.fn.css = (function(css2) { 
    return function() {
        if (arguments.length) { return css2.apply(this, arguments); }
        var attr = ['font-family','font-size','font-weight','font-style','color',
            'text-transform','text-decoration','letter-spacing','word-spacing',
            'line-height','text-align','vertical-align','direction','background-color',
            'background-image','background-repeat','background-position',
            'background-attachment','opacity','width','height','top','right','bottom',
            'left','margin-top','margin-right','margin-bottom','margin-left',
            'padding-top','padding-right','padding-bottom','padding-left',
            'border-top-width','border-right-width','border-bottom-width',
            'border-left-width','border-top-color','border-right-color',
            'border-bottom-color','border-left-color','border-top-style',
            'border-right-style','border-bottom-style','border-left-style','position',
            'display','visibility','z-index','overflow-x','overflow-y','white-space',
            'clip','float','clear','cursor','list-style-image','list-style-position',
            'list-style-type','marker-offset'];
        var len = attr.length, obj = {};
        for (var i = 0; i < len; i++) {
            obj[attr[i]] = css2.call(this, attr[i]);
        }
        return obj;
    };
})(jQuery.fn.css);

Note that this doesn't capture all possible CSS properties, particularly new ones for CSS3. Here is a list of all standard CSS and stable CSS3 properties, and here's one of hyphen-prefixed vendor-specific properties (such as -moz-border-start). If you really want all of them, you can glean them from there.

Sign up to request clarification or add additional context in comments.

4 Comments

Agreed - but I think its what the OP was asking for.
Edited to avoid polluting the jQuery.fn namespace with a css2 function. Otherwise, this is great stuff.
@gnarf Note that jQuery since 1.9 can take an array of properies in its css method so the loop is not needed there.
nice script, i see no box-shadow?
16

As of jQuery 1.8, you can do:

$( "#yourElement" ).css( "cssText" );

Which uses the underlying window.getComputedStyle( element ).cssText. That's the simplest way.

3 Comments

@EJTH I'm the author of that code inside jQuery ;-)
Thanks! You helped me solve the problem of moving stuff to a certain container in the dom without any styling getting lost! :-) I use it for some pet project, make a HTML page fall apart with box2d...
WOW! I love this! Simply the best way if you want a dump of every CSS property in existence!
3

For inline styles:

var styles = $("#someelement").attr("style");

From there, you should be able to split this string if you need to loop the styles.

To check individual styles, check the docs:

http://docs.jquery.com/CSS

2 Comments

I need also attributes that are set in css
Perhaps explain what you're trying to accomplish in your question. You may be better off checking for individual styles, or using classes and using .hasClass() - gnarf has a link below otherwise
-4
$("#div1).css("background-color");

will return the background color property of an element with id div1.

css( name )

Sorry, I don't know a way to get all the CSS attributes using jQuery.

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.