Jump to content
RAZZ

How can I extract DOM data from an element?

Recommended Posts

Hi,I am trying to get into this JavaScript thing, and I have what should be a really simple question:How can I extract DOM data, namely text color in RGB format, from an element?To be more precise, I have given the object a color like so:

<html><head><script type="text/javascript">function asignColor(){	document.getElementById('txt').style.color="rgb(0,250,0)";}</script></head><body><div Id="txt" onclick="asignColor()"> This is text </div></body></html>

So let's say that I want to change the color again, based on this information. How could I extract that?I tried searching, but I really don't have a clue about what keywords to use.Thanks alot to anyone who answers!

Share this post


Link to post
Share on other sites
<html><head><script type="text/javascript">function asignColor(){document.getElementById('txt').style.color="rgb(0,250,0)";}function getColor(){color = document.getElementById('txt').style.color;alert(color);}</script></head><body><div Id="txt" onclick="asignColor()"> This is text </div><br><input type="button" value="GET COLOR" onClick="getColor()"></body></html>

But it might not be enough, since it doesn't return anything but only after a value for the color property is already assigned.

Share this post


Link to post
Share on other sites
But it might not be enough, since it doesn't return anything but only after a value for the color property is already assigned.

[right][post="23134"][/post][/right]

[/quote]If you use onload() to set the initial color, when you click GET COLOR it will at least return that value.[codebox]<html><head><script type="text/javascript">function setColor(){document.getElementById('txt').style.color="rgb(0,0,0)";}function asignColor(){document.getElementById('txt').style.color="rgb(0,250,0)";}function getColor(){color = document.getElementById('txt').style.color;alert(color);}</script></head><body onload="setColor()"><div Id="txt" onclick="asignColor()">This is text </div><br><input type="button" value="GET COLOR" onClick="getColor()"></body></html>[/codebox]

Share this post


Link to post
Share on other sites

There can also be a loop, to check for the rgb value. Like this:

x = document.getElementsByTagName('div')for (c=0; c<x.length; c++){if (x[c].style.color == "rgb(0,250,0)"){ x[c].style.color = "somethingelse" }}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...