Computer >> Computer tutorials >  >> Programming >> Javascript

How to get the number of options in the dropdown list with JavaScript?


To get the count of options in a dropdown list, use the length property in JavaScript.

Example

You can try to run the following code to find the number of options in the drop-down −

<!DOCTYPE html>
<html>
   <body>
      <form id="myForm">
         <select id="mySelect">
            <option>One</option>
            <option>Two</option>
            <option>Three</option>
         </select>
      </form>
      <script>
         var val = document.getElementById("mySelect").length;
         document.write("<br>Options in the DropDown list: "+val);
      </script>
   </body>
</html>