3

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";
}
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Lohn Claidon
  • 405
  • 2
  • 7
  • 15

4 Answers4

8

There is no hasClass method on native DOM objects. You can either use classList:

elem.classList.add('foo')
elem.classList.contains('foo')

Or split up className:

elem.className.split(/\s+/).indexOf('foo') !== -1

classList isn't supported in IE 9 and lower, but you can get a shim if you really need it. Splitting up className with a regex will work just fine without one.

Also, I would consider trying out jQuery. You could simplify your code:

function setMenuCurrentTo(name) {
    $('.current_nav').hide();
    $('#game_' + name).show().addClass('current_nav');
}
Blender
  • 289,723
  • 53
  • 439
  • 496
6

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')
wroniasty
  • 7,884
  • 2
  • 32
  • 24
2

hasClass is a jQuery function and won't work in plain Javascript. You need to match against the element's className attribute instead:

Replace that line with:

if (getElementById("game_" + variable).className.match(/\bcurrent_nav\b/)) {} else {

This will match the element even if it contains multiple class names.

See "hasClass" with javascript? for more details.

Community
  • 1
  • 1
nullability
  • 10,545
  • 3
  • 45
  • 63
1

Use the .classList property of a DOM Element.

getElementById("game_" + variable).classList.contains("current_nav")

Patches are available for older browsers (though usually not IE6/7 if you want it on the prototype).