javascript - If element with ID has as class -
this question has answer here:
- test if element contains class? 18 answers
what have is
function setmenucurrentto(variable) { document.getelementbyid("game_" + variable).style.display = "block"; var elems = document.getelementsbyclassname("current_nav"); (var i=elems.length; i--;) { elems[i].style.display = "none"; elems[i].classname = 'none'; } document.getelementbyid("game_" + variable).classname="current_nav"; } }
so when click tag specific element(variable) adds content , "hides" one. there bug when click twice in same button, content dissapears , don't have anymore content.
so tried code:
function setmenucurrentto(variable) { document.getelementbyid("game_" + variable).style.display = "block"; if (getelementbyid("game_" + variable).hasclass("current_nav")) { } else { var elems = document.getelementsbyclassname("current_nav"); (var i=elems.length; i--;) { elems[i].style.display = "none"; elems[i].classname = 'none'; } document.getelementbyid("game_" + variable).classname="current_nav"; }
the
if (getelementbyid("game_" + variable).hasclass("current_nav")) {} else {
make code don't work, content appears no other "hides". problem in code? thank you, i'm new @ javascript, got yesterday original code. thank again.
edit:
i got correct answer: wroniasty
function setmenucurrentto(variable) { document.getelementbyid("game_" + variable).style.display = "block"; if (jquery('#game_' + variable).hasclass('current_nav')) { } else { var elems = document.getelementsbyclassname("current_nav"); (var i=elems.length; i--;) { elems[i].style.display = "none"; elems[i].classname = 'none'; } document.getelementbyid("game_" + variable).classname="current_nav"; } }
getelementbyid
returns domnode , there no hasclass
method in domnode
.
you may want use library, jquery:
jquery('#game_' + variable).hasclass('current_nav')
Comments
Post a Comment