JavaScript "example" of a switch to compare HTML button id's -
i posted question week ago how use javascript switch statement compare this.id. found hard function/object methods out of switch variables. using strict mode , trying seems impossible. did find 1 way results wanted.
"use strict" function fragmentloader() { getid(this.id); } function getid(x) { var theid = x; switch (theid) { case "myfirstid": mydate(); break; case "mysecondid": changestyle(); break; case "mythirdid": mytext(); break; default: otpt = "error!"; } } function mydate() { document.getelementbyid('content').innerhtml = date(); } function changestyle() { var whatcolor = document.getelementbyid("content").style.color; if ( whatcolor === "black") { document.getelementbyid("content").style.color = "blue"; } else { document.getelementbyid("content").style.color = "black"; } } function mytext() { document.getelementbyid('content').innerhtml = "this text display"; } document.getelementbyid("content").style.color = "black"; document.queryselector('#myfirstid').addeventlistener('click', fragmentloader); document.queryselector('#mysecondid').addeventlistener('click', fragmentloader); document.queryselector('#mythirdid').addeventlistener('click', fragmentloader); <div> <div> <button id="myfirstid"> press date , time. </button> </div> <div> <button id="mysecondid"> press change style color. </button> </div> <div> <button id="mythirdid"> press text. </button> </div> <p id="content">content here </p> </div> had laugh @ example because odd reason takes 2 clicks style change. ideas why? note : " fixed"
other hope helps else. -rob
this line reason code not work expected:
document.getelementbyid("content").style.color === "black"; you're trying set color black, used many "=" signs. change line to:
document.getelementbyid("content").style.color = "black"; ... , code work!
Comments
Post a Comment