I want to copy all the calculated css of an element and apply it to another element. By calculated style, I mean for examply if the element's width is 100%, but it's parent's width is 200px, the elements calculated width would be 200px. how can I access all of these styles for an element ?
+3
A:
Use computed-style. Here's a simple tutorial: Get Computed Style
And as for jquery, it can help you get a reference to the source and target element and apply the css rule easily. let's say you are only interested in two properties, width and height, then you can use code along the lines of:
var oSource = $('#source');
var sWidth = document.defaultView.getComputedStyle(oSource, null).width;
var sHeight = document.defaultView.getComputedStyle(oSource, null).hight;
$('#target').css('width', sWidth).css('height', sHeight);
Majid
2010-01-28 00:59:33
thanks alot.I think I have to use it with currentStyle to support IE as well, yeah ?
hakim-sina
2010-01-28 01:10:00
Not sure about i.e 7, check this link on compatibility: http://www.quirksmode.org/dom/w3c_css.html
Majid
2010-01-28 01:23:37
+1
A:
after further research using Majid's answer, I found out that Jquery's ".css" method return computed style as well, and is better because it accounts for browser difference http://api.jquery.com/css/
hakim-sina
2010-01-28 01:34:54
Yeah! Silly me! Why did I try to get the spoon to mouth taking that long route. Morale of the story: Before looking elsewhere always check the tools you already have.
Majid
2010-01-28 01:48:08