Skip to content

Instantly share code, notes, and snippets.

@andrijac
Created August 13, 2012 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrijac/3340350 to your computer and use it in GitHub Desktop.
Save andrijac/3340350 to your computer and use it in GitHub Desktop.
select element in jquery, reading values
// selected index in select (dropdown)
$.fn.selectedIndex = function () {
var index = 0;
this.each(function () {
index = +$(this).prop('selectedIndex');
return false;
});
return index;
};
// selected value in select (dropdown)
$.fn.selectedValue = function () {
var value = 0;
this.each(function () {
value = $(this).find("option:selected").val();
return false;
});
return value;
};
// select all items in listbox
$.fn.selectAllItems = function () {
this.each(function () {
$(this).find('option').attr("selected", "selected");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment