Itzik Ben Hutta
Itzik Ben Hutta

Reputation: 193

Maximum call stack size exceeded when using select2 on appended select fields

I'm appending a div that contains a select input, but when I try to initialize select2 on it I get this error and my page doesn't respond until I force quite it.

$('.page-content').on('DOMNodeInserted', '.material-container', function () {
    var selectField = $(this).find(".select-material");
    selectField.select2();
});

Here is the console error.

jquery.js?27d9:5946 Uncaught RangeError: Maximum call stack size exceeded.
at HTMLSelectElement.eval (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5946:21)
at domManip (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5759:14)
at jQuery.fn.init.after (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:5944:10)
at jQuery.fn.init.jQuery.fn.(anonymous function) [as insertAfter] (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:6052:37)
at Select2._placeContainer (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5137:16)
at new Select2 (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5063:10)
at HTMLSelectElement.eval (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5667:26)
at Function.each (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:368:19)
at jQuery.fn.init.each (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:71:1), <anonymous>:157:17)
at jQuery.fn.init.$.fn.select2 (eval at <anonymous> (http://crm.dev:3000/js/vendor.js:163:1), <anonymous>:5664:14)
at HTMLDivElement.eval (eval at <anonymous> (http://crm.dev:3000/js/app.js:149:1), <anonymous>:51:17)

Upvotes: 1

Views: 3495

Answers (1)

vothaison
vothaison

Reputation: 1694

When you init a select2 instance, ton of things are added to the DOM, DOMNodeInserted event gets fired then you have a infinite loop.

I have 2 ways to workaround this (of course there are more):

Upvotes: 1

Related Questions