'bootstrapSwitch - remove eventlistener

I would like to remove all eventlisteners from my page. For that I have an array with all the ids. I am working also with bootstrapSwitches and with them I have my problems, because after removing the eventlistener, I can add new eventlistener to all elements, but not to the bootstrapSwitch anymore.

Here is my code to remove the eventlistener:

for (let i = 0; i < elmIds.length; i++){
        $("#" + elmIds[i]).off();
}

After removing the eventlistener, setting a new one (bootstrapSwitch) doesn't work anymore:

 $("#" + id).on('mouseup', function(event, state) {
      doSomething({id: id, checked: event.target.checked});
 });

Using this code to remove eventlistener doesn´t work:

 $("#" + id).off('mouseup', function(event, state) {
      doSomething({id: id, checked: event.target.checked});
 });

It looks like, that the method elmIds[i].off() removes also eventlistener from the bootstrap code. What is the right way to handle eventlistener with bootstrapSwitches? Happy for some help.


EDIT:

To reduce the question:

How can I reattach an event to a bootstrapSwitch after removing the previous eventlistener with $(id).off()? Because with the statement $(id).off() the switch doesn't respond at all. Even if I use: $('[name="checkbox"]').bootstrapSwitch();

Using the code below does not remove the eventlistener:

 $(id).off('switchChange.bootstrapSwitch', function(event, state) {
      doSomething({id: id, checked: event.target.checked});
 });

Where is my mistake?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source