Reputation: 359
I've got a dropdown box to the left of a select/option box.
I would like for jquery to check to see if the item already exists by value. The value for each option won't be in any order.
Text box:
<select id="selectbox"> <option value="20:32">Something1</option> <option value="103:16">Something2</option> </select>
The dropdown will have a list of items that also share the same values.
Is there a way to do something like:
$("#boxbutton").click(function () {
if (('#dropdownbox :selected').val() doesn't exist inside #selectbox list)
{
...add dropdownbox item;
} else { do nothing };
});
I want to make sure the function is checking the entire selectbox value list and NOT selected/highlighted entries in the selectbox. The textbox won't always have selected entries and will need to be checked anyway.
When The user goes to the dropdown box to add another entry to the selectbox it should repeat the search.
Not having any luck using the .length
keyword. Any ideas?
Upvotes: 0
Views: 1163
Reputation: 966
Keep one Array and put all of your values into it. Then you can easily validate your text box value with that array. Or you have to get all option tag value by using DOM iteration.
Upvotes: 1
Reputation: 4320
Try this:
var value = $('#dropdownbox').val(); // :selected is unnecessary
var hasValue = ( $('#selectbox option[value="'+value+'"]').size() > 0 );
Upvotes: 3