27 Jan jQuery – Toggle an element
To toggle an HTML element, create a button and set the toggle on it using the jQuery toggle() method. In the below example, we will show and hide the <p> element i.e. toggle. Let us see the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("h2").click(function(){ $("p").toggle(); }); }); </script> </head> <body> <h1>Demo Heading</h1> <h2>Toggle Text</h2> <p>This is demo text</p> </body> </html> |
Output
No Comments