Javascript Form How to - Get options length from select dropdown








Question

We would like to know how to get options length from select dropdown.

Answer


<!--from   ww  w  .  j a  v  a 2  s .  c  o  m-->
<html>  
    <form name="myForm">
      <select name="mySelect" onChange='console.log(mySelect.options.length)'>
        <option value=A>AA</option>
        <option value=B>BB</option>
        <option value=C>CC</option>
        <option value=D>DD</option>
      </select>
    </form>
</html>

The code above is rendered as follows: