How do I reverse this JavaScript code? -


i have webpage has link set change background colour of page, colour of link, & link text. here code:

<html>  <head> <title>title</title> <script type="text/javascript"> function lights() { document.body.style.backgroundcolor='#000000'; document.getelementbyid("on").innerhtml="turn on lights"; document.getelementbyid("on").style.color="#00ffff"; } </script> </head>  <body style="font-size:200px; text-align:center;"> <a href="javascript:void(0)" id="on" onclick="lights()">turn off lights</a> </body>  </html> 

i wondering how set javascript reverse statements when link clicked again page white background, text "turn off lights", & link darker blue.

thank you

you can use variable store state , toggle based off that.

<script type="text/javascript">   var toggle = false;   function lights() {     var link = document.getelementbyid("on");     document.body.style.backgroundcolor= toggle ? '#ffffff' : '#000000';     link.innerhtml=toggle ? "turn off lights": "turn on lights";     link.style.color= toggle ? '#000000': "#00ffff";     toggle = !toggle;   } </script> 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -