newrehmi Posted May 19, 2011 Share Posted May 19, 2011 hello all!let say, i want to make a script:"if the body background is white, the change it to blue", so is my code is correct?if (document.body.style.background = white) {document.body.style.background = "blue"}sorry for the noob question, I tried to make a function based on this concept, but seems like it's not working.Thank you so very much. I need your help please. Thanks. Link to comment Share on other sites More sharing options...
Ingolme Posted May 19, 2011 Share Posted May 19, 2011 The property value is a string. And for comparing you have to use a double equals == if(document.body.style.backgroundColor == "white") Link to comment Share on other sites More sharing options...
newrehmi Posted May 19, 2011 Author Share Posted May 19, 2011 The property value is a string. And for comparing you have to use a double equals ==if(document.body.style.backgroundColor == "white") thanks for answering. and no, it's not working too... here is my code:if(document.body.style.backgroundColor == "white") {document.body.style.backgroundColor = "red";} the body color is still not changing... please help me.. tell me what is wrong...I got a second question:how to declare some html element as a variable?let say the element/object is: document.body()so, in my script: <script>var the_body = document.body()the_body.style.background = "green"</script> but, look like my code is wrong! =_+ and this variable thingy is so confused than what i learned in php... i am trying to grasp the basic. So, anyone please help me... thanks.. soo much.! Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2011 Share Posted May 19, 2011 1. document.body is not a function, so calling document.body() throws an error.2. be aware that the style property of an element is NOT set from values in a CSS style sheet. It has values only if they are set in JavaScript or embedded in an element tag, like <body style="backgroundColor: #afa;"> . If you have NOT set a style in one of these ways, a test like if(document.body.style.backgroundColor == "white") will fail. Link to comment Share on other sites More sharing options...
newrehmi Posted May 19, 2011 Author Share Posted May 19, 2011 1. document.body is not a function, so calling document.body() throws an error.2. be aware that the style property of an element is NOT set from values in a CSS style sheet. It has values only if they are set in JavaScript or embedded in an element tag, like <body style="backgroundColor: #afa;"> . If you have NOT set a style in one of these ways, a test like if(document.body.style.backgroundColor == "white") will fail.wow thank you! it clear me alot now! =) thanks! but i still not understand about the variable thingy. If there's any detailed tutorial about javascript variable, it should be good I think!.. but, now i am happy! later I will ask more if you dont mind =D!!Thanks! Link to comment Share on other sites More sharing options...
samerhannoun Posted May 19, 2011 Share Posted May 19, 2011 the following line should work with you, don't forget browser issues,, something not working with IE, may works on firefox and vise versa.this code working with firefox and chrome fine <html><head></head><body id="justsomeguy"><script type="text/javascript">//here we change the bgcolor onloaddocument.body.bgColor="lavender";</script><script type="text/javascript">//here we get the bgcolorvar color=document.getElementById("justsomeguy").bgColor;if(color=="lavender"){document.body.bgColor="pink";}</script><p>Hello World</p></body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted May 19, 2011 Share Posted May 19, 2011 bgColor is a deprecated attribute / property.If you want to set the style on the body element, give it a style attribute: <body style="background-color: white;"> If you want to actually get the value given from a CSS stylesheet you're going to need to mess with browser inconsistencies. There's getComputedStyle() and currentStyle but I prefer not to mess with those unless I really need to. Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2011 Share Posted May 19, 2011 My goodness, please do not recommend the use of old-fashioned attributes like bgcolor. CSS has limitations, but they can be worked around without taking a time machine back to 1996.EDIT: boy, I'm just typing too quickly today. Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2011 Share Posted May 19, 2011 The more typical way to test style initial values is to check for a negative. For example: if (element.style.backgroundColor != "red") { element.style.backgroundColor = "red";} It takes a bit of thought to work through the logic of such a situation, and to set up the initial conditions, but thinking is what programmers do. It could be some (slightly) more sophisticated logic is required: if (element.style.backgroundColor == "" || element.style.backgroundColor == "white") { element.style.backgroundColor = "red";} EDIT: stupid paste error corrected Link to comment Share on other sites More sharing options...
samerhannoun Posted May 19, 2011 Share Posted May 19, 2011 @Deirdre's Dadcan you give us the modern way to do that,, may this helpful for us,thank you in advance Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2011 Share Posted May 19, 2011 Rainjo, I think I posted while you were writing, so you might not have seen Post #9. It might help. Link to comment Share on other sites More sharing options...
jeffman Posted May 19, 2011 Share Posted May 19, 2011 FWIW, a much more detailed discussion of style values can be found here. There are also useful functions that can just be copy/pasted without you really having to understand them. Note that the tutorial provides links to related functions as well.Be aware that the tutorials solve what is actually a set of very complicated problems that can usually be avoided by clever programming choices. Link to comment Share on other sites More sharing options...
newrehmi Posted May 19, 2011 Author Share Posted May 19, 2011 woww thank you so much, i learned alot i think, thanks Deirdre's Dad and thanks all too!... =D Thanks for the typical method of testing style value! I will try to grasp bit by bit!If there's any more magic method of understanding, i believe it would help alott!! COOL! FWIW, a much more detailed discussion of style values can be found here. There are also useful functions that can just be copy/pasted with you really having to understand them. Note that the tutorial provides links to related functions as well.Be aware that the tutorials solve what is actually a set of very complicated problems that can usually be avoided by clever programming choices.Thanks!!! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.