Counting the number of option tags in a select tag in jQuery

JavascriptJqueryHtml

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

$('#input1 option').length;

This will produce 2.

Solution 2 - Javascript

The W3C solution:

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

Solution 3 - Javascript

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>

Solution 4 - Javascript

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.

Solution 5 - Javascript

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');
    }
}

Solution 6 - Javascript

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;
});

Solution 7 - Javascript

The best form is this

$('#example option').length

Solution 8 - Javascript

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

Solution 9 - Javascript

Another approach that can be useful.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMercyView Question on Stackoverflow
Solution 1 - Javascriptnightingale2k1View Answer on Stackoverflow
Solution 2 - Javascriptkarim79View Answer on Stackoverflow
Solution 3 - JavascriptSadikhasanView Answer on Stackoverflow
Solution 4 - JavascriptcletusView Answer on Stackoverflow
Solution 5 - JavascriptPhilipView Answer on Stackoverflow
Solution 6 - JavascriptworkdreamerView Answer on Stackoverflow
Solution 7 - JavascriptRicardo López GarcíaView Answer on Stackoverflow
Solution 8 - Javascriptuser1587804View Answer on Stackoverflow
Solution 9 - JavascriptzakView Answer on Stackoverflow