How to switch class in Jquery -


i got question switch html tag's class in jquery

now, have code like

<body>     <button id="login" class="login">login</button>       <script>         jquery(function($){              $('.logout').click(function(){                 $(this).removeclass('logout');                 $(this).addclass('login');                 $(this).text('login');                 alert('logged out, ready login');             });              $('.login').click(function(){                 $(this).removeclass('login');                 $(this).addclass('logout');                 $(this).text('logout');                 alert('logged in, ready logout');             });          });     </script> </body> 

i wonder why run $('.login').click no matter class has been.

thanks

the event staying bound after class changes. can delegate event element above in dom , run classes matching filter, so:

jquery(function($){      $(document).on('click', '.logout', function(){         $(this).removeclass('logout');         $(this).addclass('login');         $(this).text('login');         alert('logged out, ready login');     });      $(document).on('click', '.login', function(){         $(this).removeclass('login');         $(this).addclass('logout');         $(this).text('logout');         alert('logged in, ready logout');     });  }); 

here demo: http://jsfiddle.net/b4d9a/


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -