185

How do I count the number of <option>s in a <select> DOM element using jQuery?

<select data-attr="dropdown" id="input1"> <option value="Male" id="Male">Male</option> <option value="Female" id="Female">Female</option> </select> 

I want to find the number of <option> tags in the <select> DOM element, since with that I want to open the settings panel with that number of input fields with the corresponding option value from the drop-down box in it and to change it again in the preview panel.

The above drop-down box is in my preview panel which is generated by jQuery.

9 Answers 9

300
$('#input1 option').length; 

This will produce 2.

Sign up to request clarification or add additional context in comments.

Comments

63

The W3C solution:

var len = document.getElementById("input1").length; 

1 Comment

A variation to this method with modern javascript will be var len = document.querySelector("#input1").length;
32

You can use either length property and length is better on performance than size.

$('#input1 option').length; 

OR you can use size function like (removed in jQuery v3)

$('#input1 option').size(); 

$(document).ready(function(){ console.log($('#input1 option').size()); console.log($('#input1 option').length); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select data-attr="dropdown" id="input1"> <option value="Male" id="Male">Male</option> <option value="Female" id="Female">Female</option> </select>

1 Comment

This doesn't provide anything the answers from 2009 didn't also provide.
19

Your question is a little confusing, but assuming you want to display the number of options in a panel:

<div id="preview"></div> 

and

$(function() { $("#preview").text($("#input1 option").length + " items"); }); 

Not sure I understand the rest of your question.

Comments

15

In a multi-select option box, you can use $('#input1 :selected').length; to get the number of selected options. This can be useful to disable buttons if a certain minimum number of options aren't met.

function refreshButtons () { if ($('#input :selected').length == 0) { $('#submit').attr ('disabled', 'disabled'); } else { $('#submit').removeAttr ('disabled'); } } 

1 Comment

I found this to work after removing the braces $('#input1 :selected').length; supporting documentation api.jquery.com/length
7

The best form is this

$('#example option').length 

1 Comment

This code-only (low-value) solution was provided 4 years earlier by Sadikhasan. The use of length was posted by Karim79 9 years earlier. This answer can safely be purged from the page as it is completely redundant.
6

Ok, i had a few problems because i was inside a

$('.my-dropdown').live('click', function(){ }); 

I had multiples inside my page that's why i used a class.

My drop down was filled automatically by a ajax request when i clicked it, so i only had the element $(this)

so...

I had to do:

$('.my-dropdown').live('click', function(){ total_tems = $(this).find('option').length; }); 

Comments

0

This is giving the count of all the available options exactly as the question asked. Its useful if you already have the node in a variable

Comments

-1

Another approach that can be useful.

$('#select-id').find('option').length 

1 Comment

The question isn’t asking for the count of only selected <option>s.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.