tags:

views:

521

answers:

3

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

A: 

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

ScottE
I need also attributes that are set in css
newbie
if that is possible
newbie
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
ScottE
A: 
$("#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.

rahul
+4  A: 

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.css2 = jQuery.fn.css;
jQuery.fn.css = function() {
    if (arguments.length) return jQuery.fn.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]] = jQuery.fn.css2.call(this, attr[i]);
    return obj;
}
gnarf
This is huge....................
rahul
Agreed - but I think its what the OP was asking for.
gnarf