Brian J. Hakim
Brian J. Hakim

Reputation: 993

Extend Select2 defaults

I create some default select2 behavior: each select is select2

$('select').not('.notSelect2').select2({
    width: "150px",
    allowClear: true,
    placeholder: "EMPTY"
});

And now i want to change settings for some inputs:

function setSelect2Width(control, width) {
    $.extend(true, control.select2.defaults, { width: width });
}

This code do nothing for select2. How to change default settings?

jsfiddle.net/JpvDt/389

Upvotes: 0

Views: 2009

Answers (1)

Sander
Sander

Reputation: 266

The default values are exactly what the name suggests; values used when nothing else is specified. You are editing the default values after initializing, so the value will not be used for those selects (they will only be used for new selects without an width specified).

So instead do this:

function setSelect2Width(control, width) {
    $(control).width(width).select2();
}

Upvotes: 1

Related Questions