I want to show and hide a div
, but I want it to be hidden by default and to be able to show and hide it on click. Here is the code that I have made :
I realize this question is a bit dated and since it shows up on Google search for similar issue I thought I will expand a little bit more on top of @CowWarrior's answer. I was looking for somewhat similar solution, and after scouring through countless SO question/answers and Bootstrap documentations the solution was pretty simple. Again, this would be using inbuilt Bootstrap collapse
class to show/hide divs and Bootstrap's "Collapse Event".
What I realized is that it is easy to do it using a Bootstrap Accordion, but most of the time even though the functionality required is "somewhat" similar to an Accordion, it's different in a way that one would want to show hide All that the JavaScript does is makes all the other when the loaded Note: Here it goes-navbar
. Below is a simple solution to this. The anchor tags () could be navbar items and based on a collapse event the corresponding div will replace the existing div. It looks slightly sloppy in CodeSnippet, but it is pretty close to achieving the functionality-
$(".main-container.collapse").not($(this)).collapse('hide');
shown.bs.collapse
. Here's the Bootstrap documentation on Collapse Event.
main-container
is just a custom class.$(".main-container.collapse").on('shown.bs.collapse', function () {
//when a collapsed div is shown hide all other collapsible divs that are visible
$(".main-container.collapse").not($(this)).collapse('hide');
});
Toggle Foo
Toggle Bar