119

How do I check, using jQuery, how many options are there in a drop down menu?

Thanks.

Adam
  • 43,763
  • 16
  • 104
  • 144
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221

10 Answers10

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

or

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

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

user113716
  • 318,772
  • 63
  • 451
  • 440
  • `.children('option').length;` was the only way I could get the actual option count / select.length after I populated my select list dynamically via ajax in the .done({}); using .append(). Way to go! – yardpenalty.com Nov 10 '16 at 21:35
  • for the length of selected options is this correct "$('.search-select option:selected').length" – Youssef Boudaya Jun 12 '19 at 11:00
  • Regarding @yardpenalty.com 's response, the reason is because the .children() version parses the current DOM, while the second one with $('#mySelectList > option') parses the DOM that existed when the page was loaded. – mydoglixu Jan 10 '21 at 21:19
11
$("#mydropdown option").length

Or if you already got a reference to it,

$(myDropdown).find("option").length
Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
9

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

And a quick note, often you will need to do something in jquery for a very specific thing, but you first need to see if the very specific thing exists. The length property is the perfect tool. example:

   if($('#myDropDown option').length > 0{
      //do your stuff..
    } 

This 'translates' to "If item with ID=myDropDown has any descendent 'option' s, go do what you need to do.

Martin
  • 68
  • 5
Adam
  • 43,763
  • 16
  • 104
  • 144
5

Get the number of options in a particular select element

$("#elementid option").length
Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
5

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;
Community
  • 1
  • 1
Munzilla
  • 3,805
  • 5
  • 31
  • 35
4
$('#idofdropdown option').length;

That should do it.

Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141
3
alert($('#select_id option').length);
fantactuka
  • 3,298
  • 19
  • 29
3
$('#dropdown_id').find('option').length
second
  • 28,029
  • 7
  • 75
  • 76
3
$('select option').length;

or

$("select option").size()
nicholasklick
  • 1,212
  • 10
  • 14
1

With pure javascript you can just call the length on the id of the select box. It will be more faster. Typically with everything native javascript is performing better and better with modern browsers

This can be achieved in javascript by

     var dropdownFilterSite = document.querySelector( '#dropDownId' );  //Similar to jQuery

var length = dropdownFilterSite.length.

Good website for some learning

www.youmightnotneedjquery.com

A good video to watch by Todd Motto

https://www.youtube.com/watch?v=pLISnANteJY

Vatsal
  • 2,068
  • 4
  • 21
  • 24