Working with the awesome jQuery select2 plugin and an Ajax data source, I would like to know how to get the text values of the selected options.
With a normal select
element I would do something like this:
$('#myselect option:selected").each(function() {
var $this = $(this);
if ($this.length) {
console.log($this.val(), $this.text());
}
});
But that doesn't work with select2
, because the <option>
values populated by the Ajax call have no text.
Meanwhile, using .val()
as suggested in the documentation to get the selected values just returns an array of IDs, not the text of the displayed elements.
Is there any way around this?
JSFiddle here to demonstrate the problem: http://jsfiddle.net/vfa4831b/8/