//index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>jQuery Toggle Events</title> <link rel="stylesheet" href="styles.css"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <button id="toggleButton">Show/Hide Content</button> <div id="toggleContent" class="hidden"> <p>This content can be toggled on/off by clicking the button.</p> </div> <script src="script.js"></script> </body> </html> //styles.css body { font-family: Arial, sans-serif; text-align: center; padding: 50px; } button { padding: 10px 20px; font-size: 16px; cursor: pointer; } #toggleContent { margin-top: 20px; padding: 20px; background-color: lightgray; border-radius: 5px; } .hidden { display: none; } //script.js $(document).ready(function() { // Toggle content visibility when button is clicked $('#toggleButton').click(function() { $('#toggleContent').toggleClass('hidden'); }); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter