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.
New options can be added to a Select2 control programmatically by creating a new Javascript Option object and appending it to the control: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data. text, data.id, false, false); $('#mySelect2'). append(newOption).
var select2_existing_data = $('#select2_id'). select2('data'); select2_existing_data. append(new_data);
Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With