Select2 Get selected option data

Kavvson picture Kavvson · Oct 31, 2014 · Viewed 64.8k times · Source

$("#e2").select2("val") returns me the value, but I wan't to get the .$listtypes['name'] . ether from data-name or from the option tag display.

I want to get the option data-name for the selected option, how can I accomplish this?

This is my option generator

foreach($core->list_types() as $listtypes){
echo "
<option 
  data-name='".$listtypes['name'] ."' 
  value='".$listtypes['id']."'>
  ".$listtypes['name'] ."
</option>";
}

Answer

xploshioOn picture xploshioOn · Oct 31, 2014

you can use this

$("#e2 option:selected").text();