I have this two selects, where I only have to select one at a time:
<div class="col-md-12">
<div class="form-group">
<label>Geo Blacklist</label>
<select name="blacklist[]" multiple="multiple" id="blacklist"
class="form-control select2"
data-placeholder="Seleccionar uno o varios países" tabindex="1"
onchange="$('#whitelist').val([]).change();">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Geo Whitelist</label>
<select name="whitelist[]" multiple="multiple" id="whitelist"
class="form-control select2"
data-placeholder="Seleccionar uno o varios países" tabindex="1"
onchange="$('#blacklist').val([]).change();">
<option>x</option>
<option>y</option>
<option>z</option>
</select>
</div>
</div>
When I select anyone I get:
Uncaught RangeError: Maximum call stack size exceeded
at RegExp.exec ()
at [Symbol.replace] ()
at String.replace ()
at Function.camelCase (jquery.js:346:17)
at Function.style (jquery.js:6643:22)
at jquery.js:6866:12
at jQuery.access (jquery.js:4142:5)
at jQuery.fn.init.css (jquery.js:6849:10)
at Search.resizeSearch (select2.full.js:2032:18)
at DecoratedClass.resizeSearch (select2.full.js:580:32)
How can I do this?
2
Answers
Just remove
.change()
as it trigger the change event on the otherselect
and make infinite loopThis issue is with select2. When changing a
<select>
value that’s been select2-ised, you have to call.change()
to update the UI.This then triggers the 2nd
select
change event, which calls .change etc, creating an infinite loop as shown withMaximum call stack size exceeded
.You can’t simply remove the
.change()
as that would then not update the select2 UI.The solution is described in the select2 documentation
In the case of this question that would be to change
to
(and the same for whitelist)
Here’s a snippet (and fiddle) that demonstrates
change.select2
. Edit and change fromchange.select2
tochange
and you get the infinite loop.