469,349 Members | 1,584 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

Reading accumulated style for an element

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
5 1225
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
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
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
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
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.

Similar topics

6 posts views Thread by Eddie | last post: by
3 posts views Thread by Fabian | last post: by
1 post views Thread by Johs. | last post: by
reply views Thread by Marcelo Henderson via AccessMonster.com | last post: by
1 post views Thread by Armin Gajda | last post: by
2 posts views Thread by Ward Germonpr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.