question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Select2 assigning value using JQuery is not working

See original GitHub issue

Select2 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:closed
  • Created 8 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

37reactions
simplenotezycommented, Jan 8, 2016

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.

$('#select2').select2().select2('val', ['2'])
$('#select2').select2('val', ['2'])
$('#select2').select2().select2('val', 2)
$('#select2').select2().select2('val', "2")

From DOM inspector:

<select name="select2" class="form-control select2-hidden-accessible" id="teams" multiple="" style="width:100%" tabindex="-1" aria-hidden="true">
    <option value="1">Athletics Class (Men)</option>
    <option value="2">Acroyoga Class (Adults)</option>
    <option value="3">Acroyoga Class (Women)</option>
</select>

Code have not changed, and auto appending values does not work anywhere anymore.

Running version 4.0.1

Update:

$('#select2').val([1,2]).trigger('change');

Works

3reactions
sonirajcommented, 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();

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found