Question:
How can I show or hide a DIV when a button is clicked?
Answer:
To toggle the visibility of a DIV with a button, you can use either JavaScript or jQuery.
Using Pure JavaScript:
Get a reference to the button element:
var button = document.getElementById('button');
Define a function to toggle the visibility:
button.onclick = function() {
Get a reference to the DIV element:
var div = document.getElementById('newpost');
Check if the DIV is currently shown:
if (div.style.display !== 'none') {
Toggle the visibility accordingly:
div.style.display = 'none'; // Hide } else { div.style.display = 'block'; // Show } };
Using jQuery:
Select the button and bind a click event handler:
$("#button").click(function() {
Select the DIV and toggle its visibility:
$("#newpost").toggle(); });
The above is the detailed content of How Can I Show or Hide a DIV Element Using a Button Click?. For more information, please follow other related articles on the PHP Chinese website!