Monday, May 20, 2024
19
rated 0 times [  20] [ 1]  / answers: 1 / hits: 15981  / 11 Years ago, mon, may 13, 2013, 12:00:00

What I have now is



function setMenuCurrentTo(variable)
{
document.getElementById(game_ + variable).style.display = block;
var elems = document.getElementsByClassName(current_nav);

for (var i=elems.length; i--;) {
elems[i].style.display = none;
elems[i].className = 'none';
}
document.getElementById(game_ + variable).className=current_nav;
}
}


So when I click a tag with a specific element(variable) it adds a content and hides another one. But there is a bug that when I click twice in the same button, the content dissapears and I don't have anymore content.



So I tried this 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);

for (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 the code don't work, the content appears but no other hides. What is the problem in my code? Thank you, I'm very new at JavaScript, I got yesterday help for the original code. Thank you again.



EDIT:



I got the correct answer: from 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);

for (var i=elems.length; i--;) {
elems[i].style.display = none;
elems[i].className = 'none';
}
document.getElementById(game_ + variable).className=current_nav;
}
}

More From » if-statement

 Answers
104

getElementById returns a DOMNode and there is no hasClass method in DOMNode.



You may want to use a library, like jQuery:



jQuery('#game_' + variable).hasClass('current_nav')

[#78257] Saturday, May 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
analiseb

Total Points: 252
Total Questions: 96
Total Answers: 106

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
analiseb questions
;