views:

3059

answers:

3

Is there a way in jQuery to get all CSS from an existing element and apply it to another without listing them all?

I know it would work if they were a style attribute with attr(), but all of my styles are in an external style sheet.

Thank you Stackers

+3  A: 

Why not use .style of the DOM element? It's an object which contains members such as width and backgroundColor.

strager
I'm pretty sure this is the only way to get the actual styles associated with the class. (as opposed to the calculated styles which are different)
altCognito
erm..how? can you show an example?
Nimbuz
@Nimbuz, Maybe this will help: http://www.w3schools.com/jsref/dom_obj_style.asp
strager
A: 

You can use it like

var bg_color = $("#my-div").css("background-color");
alert (bg_color); //will give the background color

see more help here: http://docs.jquery.com/CSS/css#name

Geshan
I don't think you read the question properly.
alex
A: 

var styles = $("my-div").attr("style");

will give you the complete styling of the element.

ceasaro
This will only give you styles that are added inline. http://jsbin.com/ohaxi3/
alex