Can I check for existing items in a html select box while adding them. A user types a text in a input box and then clicks button to add items. But I want to check for identical items before adding. Is there a efficient way to write this script?
+1
A:
Check if $('#dropdown option[value=' + newoption + ']')
returns an option or not.
E.g.
if ($('#dropdown option[value=' + newoption + ']').length > 0) {
// Option exist.
} else {
// Option does not exist.
}
BalusC
2010-01-25 02:40:29
A:
fix typo and shorten the logical check in js...
if ($("#dropdown option[value=' + newoption + ']").length) {
// Option exist.
} else {
// Option does not exist.
}
wharsojo
2010-01-25 08:37:09
Uh, there was no means of a typo and you've now made this technically invalid. The `newoption` was supposed to be a variable.
BalusC
2010-01-25 12:33:31