jQuery select2 get value of select tag?

JqueryJquery Select2

Jquery Problem Overview


Hello friends this is my code:

<select id='first'>
  <option value='1'> First  </option>
  <option value='2'> Second </option>
  <option value='3'> Three  </option>
</select>

This is my select2 code:

$("#first").select2();

Below is code for getting selected value.

$("#first").select2('val'); // It returns first,second,three.

This is return a text like first,second,three and I want to get 1,2,3.
Means I need the value of select box, not text.

Jquery Solutions


Solution 1 - Jquery

$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.

Solution 2 - Jquery

To get Select element you can use $('#first').val();

To get the text of selected value - $('#first :selected').text();

Can you please post your select2() function code

Solution 3 - Jquery

$("#first").select2('data') will return all data as map

Solution 4 - Jquery

If you are using ajax, you may want to get updated value of select right after the selection.

//Part 1

$(".element").select2(/*Your code*/)    

//Part 2 - continued 

$(".element").on("select2:select", function (e) { 
  var select_val = $(e.currentTarget).val();
  console.log(select_val)
});

Credits: Steven-Johnston

Solution 5 - Jquery

This solution allows you to forget select element. Helpful when you do not have an id on select elements.

$("#first").select2()
.on("select2:select", function (e) {
    var selected_element = $(e.currentTarget);
    var select_val = selected_element.val();
});

Solution 6 - Jquery

Simple answer is :

$('#first').select2().val()

and you can write by this way also:

 $('#first').val()

Solution 7 - Jquery

Try this :

$('#input_id :selected').val(); //for getting value from selected option
$('#input_id :selected').text(); //for getting text from selected option

Solution 8 - Jquery

Above solutions did not work for me with latest select2 (4.0.13)

With jQuery you can use this solution to retrieve value according to documentation: https://select2.org/programmatic-control/retrieving-selections

$('#first').find(':selected').val();

Solution 9 - Jquery

$(".element").select2(/*Your code*/)
.on('change', function (e) {
     var getID = $(this).select2('data');
     alert(getID[0]['id']); // That's the selected ID :)
});

Solution 10 - Jquery

See this fiddle.
Basically, you want to get the values of your option-tags, but you always try to get the value of your select-node with $("#first").val().

So we have to select the option-tags and we will utilize jQuerys selectors:

$("#first option").each(function() {
    console.log($(this).val());
});

$("#first option") selects every option which is a child of the element with the id first.

Solution 11 - Jquery

Other answers wasn't working for me so i developed solution:

I created option with class="option-item" for easy targeting

HTML :

<select id="select-test">
<option value="5-val" id="first-id" class="option-item"> First option </option>
</select>

Then for every selected option i added display none property

CSS:

option:checked {
   display: none;
}

Now we can add change to our SelectBox to find our selected option with display none property by simple :hidden attribute

JQuery:

$('#select-test').change(function(){
//value 
    $('#select-test').find('.option-item:hidden').val();
//id
    $('#select-test').find('.option-item:hidden').attr('id');
//text
    $('#select-test').find('.option-item:hidden').text();
});

Working fiddle: https://jsfiddle.net/Friiz/2dk4003j/10/

Solution 12 - Jquery

Solution :

var my_veriable = $('#your_select2_id').select2().val();
var my_last_veriable = my_veriable.toString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRenish KhuntView Question on Stackoverflow
Solution 1 - JquerysomeshView Answer on Stackoverflow
Solution 2 - JqueryKrish RView Answer on Stackoverflow
Solution 3 - JquerywawaView Answer on Stackoverflow
Solution 4 - JqueryJohny PieView Answer on Stackoverflow
Solution 5 - JquerySteven JohnstonView Answer on Stackoverflow
Solution 6 - JqueryRajiv SharmaView Answer on Stackoverflow
Solution 7 - JqueryFahmi ImanudinView Answer on Stackoverflow
Solution 8 - JqueryMichal VrchotaView Answer on Stackoverflow
Solution 9 - JqueryGSIxzView Answer on Stackoverflow
Solution 10 - JqueryKeyNoneView Answer on Stackoverflow
Solution 11 - JqueryBartosz WasView Answer on Stackoverflow
Solution 12 - JqueryHafzullah YILDIRIMView Answer on Stackoverflow