How to count the number of option tags in a select tag in JavaScript?

Spread the love

Sometimes, we want to count the number of option tags in a select tag in JavaScript.

In this article, we’ll look at how to count the number of option tags in a select tag in JavaScript.

How to count the number of option tags in a select tag in JavaScript?

To count the number of option tags in a select tag in JavaScript, we get the length property of the select element.

For instance, we write

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

to add a select drop down.

Then we write

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

to select the drop down with getElementById.

And we get the number of options with the length property.

Conclusion

To count the number of option tags in a select tag in JavaScript, we get the length property of the select element.

Leave a Reply

Your email address will not be published.