Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected value in Select2 v4

This question is specifically aimed towards Select2 v4

How do I get the selected Value for a Select2 dropdown?

I have tried the following:

jQuery('#Dropdown').val()

jQuery('#Dropdown').select2().val()

and neither seem to give me the Value of the selected element. I have made sure that there is a selected item when running this code.

like image 981
Zapnologica Avatar asked Oct 15 '25 10:10

Zapnologica


2 Answers

select2 do not provide any direct method to get selected options attribute values. You need to get selected option element in select2 and then find its attribute ID:

jQuery('#Dropdown').select2().find(":selected").attr("id");

which can be narrowed down to:

jQuery("#Dropdown :selected").attr("id");
like image 89
Milind Anantwar Avatar answered Oct 18 '25 03:10

Milind Anantwar


Try this:

$('#Dropdown').select2('data')[0].text;
like image 20
plectranthus Avatar answered Oct 18 '25 04:10

plectranthus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!