Search code examples
javascriptloopsbootstrap-4

Is there a solution to apply method .collapse('show') to a node list from Bootstrap 4.6.x using JavaScript?


I am trying to apply the Bootstrap method of .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.

let findEl = document.querySelectorAll('.someClass');
            let i;
            for (i = 0; i < findEl.length; i++) {
                findEl[i].collapse('show');
            }

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


Solution

  • As Bootstrap use JQuery you have to instance those element as a jQuery Object

    let findEl = $('.someClass');
                let i;
                for (i = 0; i < findEl.length; i++) {
                    $(findEl[i]).collapse('show');
                }