
Counting the Number of Options in a Dropdown with jQuery
By: Dzhuneyt Ahmed
Posted · 1 min read
Outdated: This post uses jQuery, which is no longer necessary for DOM operations. The same result can be achieved with vanilla JavaScript:
document.querySelectorAll(‘#my_dropdown option’).length.
Let’s say you have this (very) simple dropdown:
United States United Kingdom
And one day you decide to figure out how much elements or “option” tags the given “select” has.
It’s easy:
var items = $('#my_dropdown option').length; alert(items);
The above should immediately display “2” since we have 2 countries in the select dropdown.
