Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 Get selected option data

$("#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>"; } 
like image 773
Kavvson Avatar asked Oct 31 '14 17:10

Kavvson


People also ask

How do I get Select2 data?

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.

How do I render Select2 options in HTML?

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).

How do I select a specific DropDownList using jQuery?

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”).

How do I add options in Select 2?

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).


1 Answers

you can use this

$("#e2 option:selected").text(); 
like image 78
xploshioOn Avatar answered Sep 19 '22 02:09

xploshioOn