0

If page is active, change class of nav li?

I've tried this:

var page = 'home';
if (page == $("#home").value) {
    $( "#NavHome" ).addClass( "active" );
};

And the html:

<li id="NavHome">
  Home
</li>

On the page itself I made this:

<input type="hidden" id="home" value="home">

I want if the page is active, the class of #NavHome is active, if not, don't set the class to active..

Can someone help me or do you have a better solution?


Those doesn't help me:

Community
  • 1
  • 1
Dirk Jan
  • 2,355
  • 3
  • 20
  • 38

2 Answers2

1

When checking the value of a jQuery object, use jQuery methods:

if (page == $("#home").val()) {
    $("#NavHome").addClass("active");
};

Demo

isherwood
  • 58,414
  • 16
  • 114
  • 157
0

Try this -

switch ($("#home").val())
{
   case "home":
         $('#NavHome').addClass('active');
         break;
}
Gjohn
  • 1,261
  • 1
  • 8
  • 12