EDIT: http://jsfiddle.net/J26Dw/ (click Add)
I want to call, for every checkbox marked with the attribute data-widget="bootstrap-switch"
, the bootstrapSwitch()
plugin. This works fine with this simple function:
$(function () {
var activator = 'data-widget="bootstrap-switch"';
$('input[type="checkbox"][' + activator + ']')
.each(function () {
$(this).bootstrapSwitch();
});
});
What if the checkbox was created/added on the fly to the DOM, i.e. clicking a button ?
I tried to catch the DOMNodeInserted
event and then call bootstrapSwitch()
on the added element, only if the .data('bootstrap-switch')
is undefined
. I get the following console error (along with a great number of checkboxes :P):
Uncaught RangeError: Maximum call stack size exceeded.
Can anyone point me to the right direction?
$('body').on('DOMNodeInserted', '[' + activator + ']', function(e) {
var el = $(e.target);
console.log(el.attr('data-widget')); // Returns "bootstrap-switch", OK
if (!el.data('bootstrap-switch')) {
el.bootstrapSwitch(); // Infinite recursion
}
});