Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save treffynnon/294741 to your computer and use it in GitHub Desktop.
Save treffynnon/294741 to your computer and use it in GitHub Desktop.
jQuery: Using and Manipulating Select Lists
//Get the currently selected option's value
$(this).val();
//Get the currently selected option's title
$(this).text();
//Set the currently selected option to the supplied value
$(this).val('value');
//Get an option with a specified value
$('option[value="value-to-search-for"]', this);
//Reset select list to first value
$(this).val(''); //this doesn't work in IE
//Reset the select list to the first value
$(this).val($('option:first', this).val());
//Reset select list to blank option
$(this).val(null);
//Add an option onto the top of a select list
$(this).prepend('<option value="Option Value">Option Name</option>');
//Add an option onto the end of a select list
$(this).append('<option value="Option Value">Option Name</option>');
//Add an option before a certain option in a select list
$('option[value="value-to-search-for"]', this).before('<option value="Option Value">Option Name</option>');
//Add an option after a certain option in a select list
$('option[value="value-to-search-for"]', this).after('<option value="Option Value">Option Name</option>');
//Remove an option from a select list by value
$('option[value="value-to-search-for"]', this).remove();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment