Reputation: 35542
i have a HTML like this. I need to get the selected value's count using the tag. How to do that using jQuery.
<select id="availability1">
<option value="Available">Available</option>
<option selected="selected" value="Scheduled">Scheduled</option>
<option value="Unavailable">Unavailable</option>
</select>
<select id="availability2">
<option value="Available">Available</option>
<option value="Scheduled">Scheduled</option>
<option selected="selected" value="Unavailable">Unavailable</option>
</select>
<select id="availability3">
<option value="Available">Available</option>
<option value="Scheduled">Scheduled</option>
<option selected="selected" value="Unavailable">Unavailable</option>
</select>
<select id="availability4">
<option value="Available">Available</option>
<option selected="selected" value="Scheduled">Scheduled</option>
<option value="Unavailable">Unavailable</option>
</select>
How to get the selected values which are Scheduled. The ids have a pattern availability(X). How to write this using jQuery. The output I expect is 2 since there are two selected tags which are Scheduled.
Thanks.
Upvotes: 2
Views: 2816
Reputation: 630389
You could do something like this using .filter()
and .length
:
var scheduled = $("select").filter(function() {
return $(this).val() == "Scheduled";
});
alert(scheduled.length);
You can give it a try here, this takes all the <select>
elements, then does a .filter()
to get the ones with a selected value of "Scheduled", then we're just alerting the .length
to see how many of those there were.
Upvotes: 4