Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to apply the Bootstrap method of
HTML
.collapse('show')
to a node list of elements but I'm facing an issue in console.log: (Uncaught TypeError: findEl[i].collapse is not a function).

Since I have a node list, I tried a loop to iterate over them but it does not seem the proper way of doing it.


What would be the best approach to apply correctly the bootstrap method?

What I have tried:

I tried this loop to iterate over elements
<pre lang="Javascript">
let findEl = document.querySelectorAll('.someClass');
            let i;
            for (i = 0; i < findEl.length; i++) {
                findEl[i].collapse('show');
            }
Posted
Updated 28-Jun-21 23:28pm

1 solution

You're getting this error because Bootstrap has a dependency on jQuery, which is where it's defining the collapse() method. This is included in the Bootstrap documentation[^]. If you want to call collapse() then you might want to use the selector instead:
JavaScript
$(.someClass').collapse('show');
 
Share this answer
 
Comments
User 15221028 29-Jun-21 5:34am    
Hello Chris and thanks for you answer!
If the code is written in JavaScript, is there a way to obtain the same thing as in jQuery?
Richard Deeming 29-Jun-21 5:35am    
NB: This is true for Bootstrap 4 and below, but no longer the case for Bootstrap 5:
Bootstrap 5 | Bootstrap Blog[^]
User 15221028 29-Jun-21 5:37am    
Unfortunately I have to use an older Bootstrap version.
Is there a solution for using JavaScript instead of jQuery to apply method .collapse('show')?
Richard Deeming 29-Jun-21 5:43am    
Not for BS4 or earlier - you'd have to re-implement the functionality in your own pure Javascript method.
User 15221028 29-Jun-21 5:48am    
Thanks a lot for your support!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900