jQuery Method How to - Use toggle() Handle three states toggle click event








Question

We would like to know how to use toggle() Handle three states toggle click event.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--   w ww .  j  a  va  2s . c  o m-->
    $(".show").toggle(function(){ 
      console.log('1st'); 
    }, function() { 
      console.log('2nd'); 
    }, function(){
      console.log('3rd');
    }); 
});
</script>
</head>
<body>
  <div class="show">Click me</div>
</body>
</html>

The code above is rendered as follows: