Select and set an option within an optgroup - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:OptionGroup

Description

Select and set an option within an optgroup

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <select name="dob_year"> 
         <optgroup label="Morning"> 
            <option value="1980">80</option> 
            <option value="1981">81</option> 
            <option value="1982">82</option> 
            <option value="1983">83</option> 
         </optgroup> 
      </select> 
      <script type="text/javascript">

document.querySelector('select').selectedIndex = 2;

      </script>  
   </body>//w  w w.  j a  va 2  s.c  om
</html>

Related Tutorials