$("#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>"; }
Using the data methodCalling select2('data') will return a JavaScript array of objects representing the current selection. Each object will contain all of the properties/values that were in the source data objects passed through processResults and templateResult callbacks.
The key here for me is to build a data array with content for both templateSelection and templateResult . The latter renders fine in the dropdown but any multiline content will not be contained in the select2 element so needs to be displayed inline (or at least on a single line).
Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).
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).
you can use this
$("#e2 option:selected").text();
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