jQuery Method How to - Use fadeToggle() to hide and show elements








Question

We would like to know how to use fadeToggle() to hide and show elements.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<style type='text/css'>
.maincontent {<!--from  www. ja  v  a 2  s.  co m-->
  display: none;
}
</style>
</head>
<body>
  <div id="block4">Click me</div>
  <div class="maincontent">maincontent</div>
  <script type='text/javascript'>
    $('#block4').click(function(){
        $(".maincontent").fadeToggle(500);
    });
    
    </script>
</body>
</html>

The code above is rendered as follows: