jquery - Check if nav has already class if not add one -


i reached confusing point in code. here's thing.

$(".main-menu").click(function(e) {         e.preventdefault();          $('nav').toggleclass("light-mode");  }); 

basically want add click function ability: 1. check if nav has class "light-mode" if not, add one. 2. after user clicks second time trigger ".main-menu" want delete added class "light-mode" want more advance toggleclass.

edit: guys effort. still dont have complete solution of thing trying do. english not describe myself clear enough. have "hamburger" menu 1 click function - trigger show overlayed menu, , hide it. while click on have add class "light-mode" nav, , after second click toggler must remove class no matter what. , hard thing dont know how using 1 .click function.

toggleclass works good, if nav on specific page section has class "light-mode", toggleclass turns back. when overlay menu shows nav don't have class added. that's why cant use toggler. examples half good, on second click not removing class.

you need .hasclass(), .addclass() , .removeclass().

then:

$(".main-menu").click(function(e) {   e.preventdefault();   if ($('nav').hasclass("light-mode")) {     $('nav').removeclass("light-mode");   } else {     $('nav').addclass("light-mode");   } }); 

working demo.

$(".btn").click(function(e) {    e.preventdefault();    if ($('.example').hasclass("red")) {      $('.example').removeclass("red");    } else {      $('.example').addclass("red");    }  });
.example {    width:200px;    height:200px;    background-color:blue;  }    .red {    background-color:red;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <button class="btn">  button  </button>    <div class="example"></div>

you build small plugin (tutorial) if want use more times:

(function( $ ){    $.fn.classtoggler = function(classtocheck, classtoaddorremove) {     if ($(this).hasclass(classtocheck)) {       $(this).removeclass(classtoaddorremove);   } else {       $(this).addclass(classtoaddorremove);   }    };  })( jquery ); 

and:

$(".btn").click(function(e) {   e.preventdefault();   $('.example').classtoggler("red", "red"); }); 

updated demo.

(function( $ ){     $.fn.classtoggler = function(classtocheck, classtoaddorremove) {        if ($(this).hasclass(classtoaddorremove)) {      $(this).removeclass(classtoaddorremove);    } else {      $(this).addclass(classtoaddorremove);    }     };   })( jquery );    $(".btn").click(function(e) {    e.preventdefault();    $('.example').classtoggler("red", "red");  });
.example {    width:200px;    height:200px;    background-color:blue;  }    .red {    background-color:red;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <button class="btn">  button  </button>    <div class="example"></div>

p.s. since trying build "togler" have use .hasclass() because need both .addclass() , .removeclass() inside same .click() function , delete each other without if statement.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -