Created
February 4, 2010 15:19
-
-
Save treffynnon/294741 to your computer and use it in GitHub Desktop.
jQuery: Using and Manipulating Select Lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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