jQuery select2 get value of select tag?

Renish Khunt picture Renish Khunt · Nov 11, 2013 · Viewed 240.6k times · Source

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.

Answer

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