jQuery select2 get value of select tag?
Solution 1:
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.
Solution 2:
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:
$("#first").select2('data')
will return all data as map
Solution 4:
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:
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();
});