Select2: when setting options, the first item is auto-selected

Created on 8 Jul 2016  路  3Comments  路  Source: select2/select2

// select dom element
var e = $(element);
var oldval = e.val(); // oldval: null
// assign new array of objects
e.select2({ data: ko.unwrap(items || [])});
// e.val(): id of the first array item
// as a workaround, i now have to set to oldval
e.val(oldval); 

i am using the latest select2 version.
this behavior was changed with the last update.
maybe you could introduce a new option like 'autoassign: false'
to have the old behavior back.
the value of my dropdown can be empty.
i am using this in a dependent dropdown scenario with knockoutjs.
i only want values to be assigned if chosen.

maybe opposite to #2493 or #2196

Most helpful comment

You can get the old behaviour back by adding a blank option (<option></option>) to your select. This is being done by the browser, not by Select2.

All 3 comments

You can get the old behaviour back by adding a blank option (<option></option>) to your select. This is being done by the browser, not by Select2.

on select2 with thumbnail it tries to get thumbnail image for blank option with the mydomain.com/page/undefined url

Also, you need to set placeholder for the empty option to not show up under dropdown options.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ethanclevenger91 picture ethanclevenger91  路  3Comments

dereuromark picture dereuromark  路  3Comments

videsignz picture videsignz  路  3Comments

eved42 picture eved42  路  3Comments

JuanWilde picture JuanWilde  路  3Comments