By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,459 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,459 IT Pros & Developers. It's quick & easy.

Reading accumulated style for an element

P: n/a
Joe
Hi,

This may seem like a strange thing to want to do, but here goes ...

Given an arbitrary element in an HTML page (eg. <td id="bob">...), is
there a method I could use to fetch the accumulated style that is
currently being applied to that tag?

I then want to apply that overall style to another tag (as I'm lifting
the HTML contained within the identified tag and transplanting it to
another, and want to dynamically carry over the style so it ends up
looking the same).

Incidentally, this only needs to work on IE6+.
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Joe wrote:
Hi,

This may seem like a strange thing to want to do, but here goes ...

Given an arbitrary element in an HTML page (eg. <td id="bob">...), is
there a method I could use to fetch the accumulated style that is
currently being applied to that tag?

I then want to apply that overall style to another tag (as I'm lifting
the HTML contained within the identified tag and transplanting it to
another, and want to dynamically carry over the style so it ends up
looking the same).

Incidentally, this only needs to work on IE6+.


getComputedStyle comes to mind, is that IE? (shuffles stuff around and
finds The Bible: Dynamic HTML by Danny Goodman, O'Reilly & Associates,
ISBN 0-596-00316-1) No. IE uses currentStyle. If you want only
explicitly assigned properties, use runtimeStyle (windows version only).

--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #2

P: n/a
jo*@canfactory.com (Joe) wrote:
Hi,

This may seem like a strange thing to want to do, but here goes ...

Given an arbitrary element in an HTML page (eg. <td id="bob">...), is
there a method I could use to fetch the accumulated style that is
currently being applied to that tag?
Use currentStyle instead of style.
<URL:
http://msdn.microsoft.com/workshop/a...rrentstyle.asp
/>
I then want to apply that overall style to another tag (as I'm lifting
the HTML contained within the identified tag and transplanting it to
another, and want to dynamically carry over the style so it ends up
looking the same).

Incidentally, this only needs to work on IE6+.


Maybe
var element1 = document.getElementById("element1");
var element2 = document.getElementById("element2");
for ( var style in element1.currentStyle )
element2.runtimeStyle[style] = element1.currentStyle[style];

Regards,
Steve
Jul 20 '05 #3

P: n/a
you can easily loop through everything in the style object and look for what
you need .. i think for ns youll need the prop CSSText or something, anyway:

heres the loop:
it gets your 'TD id=bob' and then builds a string of all style info for bob
and then dumps in a form.textarea

var myObj = document.getElementById('bob')

for (i in myObj.style){
foo += i;
foo += ': ';
foo += this.style[i];
foo += '\n';
}
document.forms[0].elements[0].value = foo;

<textarea></textarea>

PIeter.


"Joe" <jo*@canfactory.com> wrote in message
news:de**************************@posting.google.c om...
Hi,

This may seem like a strange thing to want to do, but here goes ...

Given an arbitrary element in an HTML page (eg. <td id="bob">...), is
there a method I could use to fetch the accumulated style that is
currently being applied to that tag?

I then want to apply that overall style to another tag (as I'm lifting
the HTML contained within the identified tag and transplanting it to
another, and want to dynamically carry over the style so it ends up
looking the same).

Incidentally, this only needs to work on IE6+.

Jul 20 '05 #4

P: n/a
Joe
Excellent - thanks very much for all the replies.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5

P: n/a
jo*@canfactory.com (Joe) writes:
Given an arbitrary element in an HTML page (eg. <td id="bob">...), is
there a method I could use to fetch the accumulated style that is
currently being applied to that tag?
There are two ways: The standard way and IE's way :)

In IE, the element has a "currentStyle" property, i.e.,
document.getElementById("bob").currentStyle

The standard way is more convoluted. There, you do:
document.defaultView.getComputedStyle(document.get ElementById("bob"),"")
(the "" at the end could also be, e.g., "after" to get the properties of
#bob:after {color:red;}
There is no corresponding feature for IE)

Mozilla/Gecko and Opera 7.2 implements the standard way. I don't know
if there are others.
Incidentally, this only needs to work on IE6+.


So, you can get away with only using currentStyle.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.