Skip to content

Instantly share code, notes, and snippets.

@venblee
Created June 21, 2013 18:01
Show Gist options
  • Save venblee/5833066 to your computer and use it in GitHub Desktop.
Save venblee/5833066 to your computer and use it in GitHub Desktop.
Get Value or Text from Select2
var test = $('#test');
$(test).select2({
data:[
{id:0,text:"enhancement"},
{id:1,text:"bug"},
{id:2,text:"duplicate"},
{id:3,text:"invalid"},
{id:4,text:"wontfix"}
],
width: "300px"
});
$(test).change(function() {
//var theID = $(test).val(); // works
//var theSelection = $(test).filter(':selected').text(); // doesn't work
var theID = $(test).select2('data').id;
var theSelection = $(test).select2('data').text;
$('#selectedID').text(theID);
$('#selectedText').text(theSelection);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment