views:

115

answers:

9

How do I check using jquery how many options are there in a drop down?

thanks.

+2  A: 
alert($('#select_id option').length);
fantactuka
+2  A: 
$('#idofdropdown option').length;

That should do it.

Rob Stevenson-Leggett
+3  A: 
$("#mydropdown option").length

Or if you already got a reference to it,

$(myDropdown).find("option").length
Matti Virkkunen
+9  A: 
var length = $('#mySelectList').children('option').length;

or

var length = $('#mySelectList > option').length;

This assumes your <select> list has an ID of mySelectList.

patrick dw
+2  A: 
$('#dropdown_id').find('option').length
second
+4  A: 

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
Adam
+2  A: 
$('select option').length;

or

$("select option").size()
nicholasklick
`.size()` is useful if you want to chain
adardesign
.size() returns and integer and, therefore, is not chain-able. You can only chain methods that return jQuery objects.
Russ Bradberry
+2  A: 

Get the number of options in a particular select element

$("#elementid option").length
Shaji
There is no advantage to using ".size" over ".length" which is slightly faster.
Russ Bradberry
Thanks @Russ Bradberry I will update the answer.
Shaji
+2  A: 

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;
Munzilla
see my comment to @Shaji
Russ Bradberry
thanks, didn't realize length was faster. Updating...
Munzilla