I'm quite new to all this but I learned quite quickly, as it's really easy in my opinion. I'm trying to make a button hide a <p>
tag that another button spawned. How can make the button (At the bottom of code) make the <p>
tag disappear?
<button type="button" onclick="document.getElementById('date_button').innerHTML = Date()"> Click this button to display the current time!</button><br> <p id="date_button"></p><br><br> <button onclick="myFuction()">Click this to hide date.</button>
You have to define the called function and using display:none hide the paragraph
function myFuction() { document.getElementById('date_button').style.display = 'none' }
<button type="button" onclick="document.getElementById('date_button').innerHTML = Date();document.getElementById('date_button').style.display='block'"> Click this button to display the current time!</button><br> <p id="date_button"></p><br><br> <button onclick="myFuction()">Click this to hide date.</button>
Try this. I think this can help
function myFuction() {
document.getElementById('date_button').style.display = 'none';
}
In your case you want to show it again so you need to set its innerHTML
to ''
. In your case if you will do style.display = 'none'
. It will not show <p>
again
function myFunction(){ document.getElementById('date_button').innerHTML = ''; }
<button type="button" onclick="document.getElementById('date_button').innerHTML = Date()"> Click this button to display the current time!</button><br> <p id="date_button"></p><br><br> <button onclick="myFunction()">Click this to hide date.</button>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.