Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle show/hide div with button?

Hopefully this is an easy question. I have a div that I want to toggle hidden/shown with a button

<div id="newpost"> 
like image 236
user547794 Avatar asked Dec 24 '10 19:12

user547794


People also ask

How do I hide and show a div on click?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

How can show hide div on button click in bootstrap?

You need to make sure the target div has an ID. Bootstrap has a class "collapse", this will hide your block by default. If you want your div to be collapsible AND be shown by default you need to add "in" class to the collapse. Otherwise the toggle behavior will not work properly.


1 Answers

Pure JavaScript:

var button = document.getElementById('button'); // Assumes element with id='button'  button.onclick = function() {     var div = document.getElementById('newpost');     if (div.style.display !== 'none') {         div.style.display = 'none';     }     else {         div.style.display = 'block';     } }; 

SEE DEMO

jQuery:

$("#button").click(function() {      // assumes element with id='button'     $("#newpost").toggle(); }); 

SEE DEMO

like image 138
Andrew Whitaker Avatar answered Oct 13 '22 01:10

Andrew Whitaker