Created
March 5, 2010 21:22
-
-
Save nakajima/323166 to your computer and use it in GitHub Desktop.
Choose an option from a select by text
This file contains 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
// Usage: | |
// | |
// $('#the-select').choose('Some Option'); | |
// | |
// It'll throw an error if the option doesn't exist. | |
$.fn.choose = function(name) { | |
var elem = $(this); | |
if (elem.is(':not(select)')) { return elem; } | |
var option = elem.find('option').filter(function() { | |
return name == $.trim($(this).text()); | |
}); | |
if (option.size()) { | |
option.attr('selected', 'selected'); | |
} else { | |
throw(new Error(name + ' is not a valid option!')); | |
} | |
return elem; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment