Select2 assigning value using JQuery is not working
See original GitHub issueSelect2 4.0.1, assigning value using JQuery is not working. Previously I used
$('select').select2().select2('val', '3');
was working fine but now it’s not working.
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
With jQuery, assigning value to Select2 select dropdown is not ...
I have a checkbox that when checked, assign a value to a drop down select box. Problem is, the select drop down is...
Read more >Add, select, or clear items | Select2 - The jQuery replacement ...
For Select2 controls that receive their data from an AJAX source, using .val() will not work. The options won't exist yet, because the...
Read more >Options - Select2 - JeeSite
The value of the option is subject to jQuery's parsing rules for HTML5 data attributes. Can I use Select2 with my AMD or...
Read more >How to change selected value of a drop-down list using jQuery?
With jQuery, it is easy to writing one line of code to change the selected value from a drop-down list. Suppose, you have...
Read more >Select2 does not work with some values of advagg - Drupal
advagg_mod is enabled. Select "Move all external scripts to the top of the execution order". Two errors show up: Uncaught ReferenceError: jQuery ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No that is not the issue. I have exactly the same issue. I guess it happend after recent changes to select2 codebase.
The select2 is created in dom with all options. I have verified that the values we are trying to select is there in <option>. It does not work.
From DOM inspector:
Code have not changed, and auto appending values does not work anywhere anymore.
Running version 4.0.1
Update:
Works
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();