RAZZ Posted May 15, 2006 Share Posted May 15, 2006 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! Link to comment Share on other sites More sharing options...
skym Posted May 15, 2006 Share Posted May 15, 2006 <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. Link to comment Share on other sites More sharing options...
scott100 Posted May 15, 2006 Share Posted May 15, 2006 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] Link to comment Share on other sites More sharing options...
RAZZ Posted May 17, 2006 Author Share Posted May 17, 2006 Thanks alot guys. That's very useful info.Too bad the browsers don't work more directly with the DOM. Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 17, 2006 Share Posted May 17, 2006 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" }} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now