How do I check using jquery how many options are there in a drop down?
thanks.
How do I check using jquery how many options are there in a drop down?
thanks.
$('#idofdropdown option').length;
That should do it.
$("#mydropdown option").length
Or if you already got a reference to it,
$(myDropdown).find("option").length
var length = $('#mySelectList').children('option').length;
or
var length = $('#mySelectList > option').length;
This assumes your <select>
list has an ID of mySelectList
.
Use the length property or the size method to find out how many items are in a jQuery collection. Use the descendant selector to select all <option>
's within a <select>
.
HTML:
<select id="myDropDown">
<option>1</option>
<option>2</option>
.
.
.
</select>
JQuery:
var numberOfOptions = $('select#myDropDown option').length
Get the number of options in a particular select element
$("#elementid option").length
Click here to see a previous post about this
Basically just target the ID of the select and do this:
var numberOfOptions = $('#selectId option').length;