I have a navbar with several buttons that make divs appear underneath.
<a data-bs-toggle="offcanvas" href="#select" role="button" aria-controls="select"></a>
<div id="select" class="offcanvas offcanvas-start" tabindex="-1" data-bs-backdrop="false">
Foobar
</div>
I also need to open these windows programmatically. For example, if I initially use $('#select').collapse('show')
nothing happens.
However, if I first click on the A tag and then execute the line of code $('#select').collapse('toggle')
I am able to hide/show the window. However, if I try to click on the A tag again, it tells me
Bootstrap doesn’t allow more than one instance per element. Bound instance: bs.collapse
Roughly speaking, I should add the class "show" to the div with id="select" and then set "visibility: visible"; to the div. However then clicking again on the A tag skips a loop, and I have to click twice to make the div disappear again.
3
Answers
In JQuery you can show elements using
$("#something").show();
and hide with$("#something").hide();
You could do something like this-
Keep track of a flag where it changes to true or false each time you click. Use the CSS properties in the function whether to display or not.
HTML-
In tag,
From your code, it seems like you are mixing Bootstrap’s
collapse
andoffcanvas
functionalities.Since you’re using
offcanvas
, you should use Bootstrap’soffcanvas
API methods rather than thecollapse
API.For example: