HTML / Javascript - Trouble changing text when clicked -


so have html code here:

<body> <b style="font-size: 26px;">how game works</b> <u id="howtoplay_hideshow" style="color: #9ff;">[hide]</u><br> </body> 

and used javascript turn hide text show, , show hide when clicked on.

<script> var howgameworks_hidden = false; document.getelementbyid("howtoplay_hideshow").onclick = function () {     if (howgameworks_hidden == false) {         document.getelementbyid("howtoplay_hideshow").innerhtml = "[show]";         howgameworks_hidden = true;     }     if (howgameworks_hidden == true) {         document.getelementbyid("howtoplay_hideshow").innerhtml = "[hide]";         howgameworks_hidden = false;     } } </script> 

this, however, not seem work. clicking on hide , show text has no effect @ all. tried removing piece of code:

if(howgameworks_hidden == true) {     document.getelementbyid("howtoplay_hideshow").innerhtml = "[hide]";     howgameworks_hidden = false;   } 

and correctly turns hide text show when click (but, of course, not turn show text hide).

so how code working?

this because second if statement triggered if first 1 does, since set howgameworks_hidden true in it. need use else:

if(howgameworks_hidden == false) {   document.getelementbyid("howtoplay_hideshow").innerhtml = "[show]";   howgameworks_hidden = true; }  else if(howgameworks_hidden == true) {   document.getelementbyid("howtoplay_hideshow").innerhtml = "[hide]";   howgameworks_hidden = false; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -