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

HTMLJavascriptProgramming Scripts

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 −

Live Demo

<!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>
raja
Published on 26-Feb-2018 13:20:22
Advertisements