473,396 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1322
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eddie | last post by:
When I use JavaScript to read an element's textDecoration style, I only get one value even if there are more than one in the sytle sheet. For example if the text-decoration is defined as:...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
3
by: Fabian | last post by:
How can I read the object.style.display attribute? With teh following: var x = object.style.display; I always get a result of undefined. Solutions? -- -- Fabian
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
1
by: Johs. | last post by:
Hello I have a table with dates and numbers. I create sums pr. years with a query, but how can I create accumulated sum in the column accumulated? Accumulate sums Year Sum Accumulated
2
by: awoodgate | last post by:
Hi, I'm trying to read the width of a css absolutely positioned element in javascript. If I use an id selector or a class definition then the width is null, but if I use an inline style then it...
0
by: Marcelo Henderson via AccessMonster.com | last post by:
I have a table “IGPM”, with three fields: “DATE”, ““ACCUMULATED” , " INDEX”. The date is my primary key, the index stores the IGP of the index of the month, and accumulated it must...
1
by: Armin Gajda | last post by:
Hi, I have the following problem: An input field get a border assigned by a style class (e.g. 2px solid red). When the field gets the focus, we set the border to green. element.style.border...
2
by: Ward Germonpr | last post by:
Hi, I know you can position an element using a style property like so : element.style.left=200px; But how can you READ an elements style.left property ?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.