How to use jQuery.toggle() method in jQuery?


The toggle() method toggles displaying each of the set of matched elements.

Example

You can try to run the following code to learn how to work with jQuery.toggle() method:

Live Demo

<html>

   <head>
      <title>jQuery toggle() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
   
         $(document).ready(function() {

            $("#toggle").click(function(){
               $(".target").toggle( );
            });
         });
           
      </script>
       
      <style>
         p {
             background-color:#bca;
             width:200px;
             border:1px solid green;
         }
      </style>
   </head>
   
   <body>

      <p>Click on the following button:</p>
      <button id = "toggle"> Toggle </button>

      <div class = "target">
         <img src = "../images/jquery.jpg" alt = "jQuery" />
      </div>
 
   </body>
</html>

Updated on: 12-Dec-2019

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements