Proper handling/creating of bootstrap classes in pure javascript? (V5) #31715
-
Fairly new to bootstrap (and web dev in general) so my thoughts and implementation may not be completely accurate. The standard way of implementing Collapse is to use data- tags (data-toggle="collapse" href="#collapseExample") Now, I'm working with a lot of generated elements and a complex scenario where I need to handle this manually in javascript. However, the second time the element is clicked, it's creating a new bootstrap.Collapse(). Now there seems to be two instances on the same element. Things can start behaving badly at this point, especially when I go to implement a "show all/ hide all" feature. To get around this, when the click is triggered I then call bootstrap.Collapse.getInstance() and only if there's no existing instance do I then create a new one. Ideally it seems like there should be a "getInstanceOrCreate" function to handle this for me, but given that it doesn't exist, I'm probably implementing this wrongly. This behavior exists with bootstrap.Modal as well, except when you have two instances bound to the same element, hide() and show() stop working at all. Please advise, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @MasterEvilAce, You shouldn't try to create a new Collapse on each click, instead you should create them when the DOM is ready or when your component is rendered. Like that you won't have to deal with : "Should I create a Collapse ? Or should I get an existing one ?" because you'll know the collapse already exist |
Beta Was this translation helpful? Give feedback.
Hi @MasterEvilAce,
You shouldn't try to create a new Collapse on each click, instead you should create them when the DOM is ready or when your component is rendered. Like that you won't have to deal with : "Should I create a Collapse ? Or should I get an existing one ?" because you'll know the collapse already exist