Write a JavaScript function to show a div that is hidden for 1000 secods when a button…
- User
Write a JavaScript function to show a div that is hidden for 1000 secods when a button with a specific class is clicked.
- Assistant
Here is a sample JavaScript function that achieves this:
html<!DOCTYPE html> <html> <head> <style> .hidden { display: none; } </style> </head> <body> <button class="show-button">Show Hidden Div</button> <div class="hidden" id="hiddenDiv">This div will be shown for 1000 seconds.</div> <script> // Function to show the hidden div for 1000 seconds function showDiv() { var hiddenDiv = document.getElementById('hiddenDiv'); hiddenDiv.style.display = 'block'; setTimeout(function() { hiddenDiv.style.display = 'none'; }, 1000000); // 1000 seconds } // Event listener for button click document.querySelector('.show-button').addEventListener('click', showDiv); </script> </body> </html> - User
There may be more than one button with class "show-button"
- Assistant
To handle the case where there may be multiple buttons with the class "show-button", you can modify the event listener to loop through all elements with that class and add the click event to each one. Here's how you can update the code:
html