Using latest select2 with multiple options. When user selects any option, I need to make arrays of selected option values and texts. To get value, I use:
var id=$('#select2_dropdown').val();
It makes arrays like id[]="value1", id[]="value2"
, etc.
Now I need to make the same array for text between <option>
tags. Found a solution at Get selected text from a drop-down list (select box) using jQuery so added this code:
var name=$("#select2_dropdown option:selected").text();
While it works great for single select, trying to use it with multiple select just makes a string like name=optiontext1optiontext2
(strings of all selected options combined into one)
How can I make it to look this: name[value1]=optiontext1, name[value2]=optiontext2
?
Tried this without any luck:
var id=$("#select2_dropdown option:selected").text();
var name[id]=$("#select2_dropdown option:selected").text();