Hi all, How can I, using JQuery, check if a value belongs to dropdown list or not? Thanks in advance.
+1
A:
Use the Attribute Equals Selector
var thevalue = 'foo';
var exists = 0 != $('#select-box option[value='+thevalue+']').length;
If the option's value was set via Javascript, that will not work. In this case we can do the following:
var exists = false;
$('#select-box option').each(function(){
if (this.value == 'bar') {
exists = true;
return false;
}
});
Lachlan Roche
2010-02-12 01:08:40
The explicit second approach is also safer as it allows option values containing any character, including `]` and `\\`. Avoid building selector strings from raw text without doing proper CSS-escaping.
bobince
2010-02-12 01:28:08