Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selected="selected" not working as expected with 4.0.1 #3990

Closed
harhoo opened this issue Dec 4, 2015 · 8 comments
Closed

selected="selected" not working as expected with 4.0.1 #3990

harhoo opened this issue Dec 4, 2015 · 8 comments

Comments

@harhoo
Copy link

harhoo commented Dec 4, 2015

Have just run into an issue where an upgrade to 4.0.1 has changed functionality over 4.0.0. Not sure if this is expected behaviour. Previously this worked:

<select class="dropdown">
<option value="100" selected="selected">Existing Choice</option>
<option value="">No Choice</option>
</select>

<script>
$(function() {
    $(".dropdown").select2({
        data: date_options,
        theme: "bootstrap"
    })
});
</script>

where date_options is [{"id":99,"text":"Some Text","type_id":1},{"id":100,"text":"Existing hoice","type_id":5}] etc.

Now, under 4.0.1, the No Choice option is always selected (doesn't matter whether it's first or second in the list).

Downgrading back to 4.0.0 fixes the issue.

@harhoo
Copy link
Author

harhoo commented Dec 4, 2015

Fiddle displaying behaviour: http://jsfiddle.net/harhoo/9k7o9per/6

@kevin-brown
Copy link
Member

At this point, after seeing all of the tickets, this is definitely a bug.

@champsupertramp
Copy link

I'm also encountering similar issue.

@lsv
Copy link

lsv commented Dec 9, 2015

+1

@kevin-brown kevin-brown added this to the 4.0.2 milestone Jan 3, 2016
kevin-brown added a commit that referenced this issue Jan 3, 2016
This adds a broken test that demonstrates the issue seen in
#3990 where existing selected
options are being reset once Select2 is initialized. This issue cannot
be reproduced on the options page [1] because the issue only appear to
happen if the selected option is not the first one in the list of
possible options.

[1]: https://select2.github.io/examples.html#data-array
@kevin-brown
Copy link
Member

This should have been fixed by d1ed0a5.

tzellman pushed a commit to tzellman/select2 that referenced this issue Feb 2, 2016
This adds a broken test that demonstrates the issue seen in
select2#3990 where existing selected
options are being reset once Select2 is initialized. This issue cannot
be reproduced on the options page [1] because the issue only appear to
happen if the selected option is not the first one in the list of
possible options.

[1]: https://select2.github.io/examples.html#data-array
@cknoxrun
Copy link

cknoxrun commented Nov 8, 2017

Hello, this fix seems to just be the failing test -- I am still experiencing the issue in 4.0.6, did I miss something? I am adding an initial option tag with a select that uses ajax. Thank you!

@andreidiaconescu
Copy link

for me,
issue reproduces also for setting the data from ajax:
{
"results": [
{
"id": 1,
"text": "Option 1"
},
{
"id": 2,
"text": "Option 2",
"selected": true
}
]
}
the selected option is not shown as selected.

Any idea what i could do ?
Thank you.

@soniraj
Copy link

soniraj commented Feb 13, 2018

A very simple way to tackle this problem is :

//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option $('#my_id option').eq(0).prop('selected',true);

//Step2: Now reinitialize your select box

//This will work, if you haven't initialized selectbox $('#my_id').select2();

or

//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it $('#my_id').select2('destroy').select2();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants