title: set a html option selected in a dropdown containing optgroup tag using javascript
body: <p>I'm working With Html(JSP) and Javascript. I have a dropdown box with id "examcenter" containing many optgroup as shown in the following code. in the function loadDrivingSchool() in javascript, I would like to set selected the value of the selected element. the loadDrivingSchool() function calls a controller in a server and when I return the view, the dropdown list does not have a selected value. I want to set this selected value to the value that the user choose before the reloading of the page. I have try the following Javascript code but it is not working:<br>
         <strong>document.getElementById('examcenter').getElementsByTagName('option')[examCenter].selected = 'selected' ;</strong></p>
  
  <pre><code>    &lt;select id="examcenter" onchange="loadDrivingSchool();"&gt;
          &lt;optgroup label="ADAMAOUA"&gt;
                  &lt;option value="1"&gt;TIBATI&lt;/option&gt;
                  &lt;option value="2"&gt;TIGNERE&lt;/option&gt;
                  &lt;option value="3"&gt;MEIGANGA&lt;/option&gt;
                  &lt;option value="4"&gt;BANYO&lt;/option&gt;
                  &lt;option value="5"&gt;NGAOUNDERE&lt;/option&gt;
          &lt;/optgroup&gt;
  
          &lt;optgroup label="CENTRE"&gt;
                  &lt;option value="6"&gt;YAOUNDE&lt;/option&gt;
                  &lt;option value="7"&gt;ESEKA&lt;/option&gt;
                  &lt;option value="8"&gt;AKONOLINGA&lt;/option&gt;
                  &lt;option value="9"&gt;NANGA EBOKO&lt;/option&gt;
                  &lt;option value="10"&gt;MONATELE&lt;/option&gt;
                  &lt;option value="11"&gt;MBALMAYO&lt;/option&gt;
                  &lt;option value="12"&gt;MFOU&lt;/option&gt;
                  &lt;option value="13"&gt;NGOUMOU&lt;/option&gt;
                  &lt;option value="14"&gt;BAFIA&lt;/option&gt;
                  &lt;option value="15"&gt;NTUI&lt;/option&gt;
          &lt;/optgroup&gt;
          &lt;optgroup label="EXTREME NORD"&gt;
                  &lt;option value="20"&gt;MAROUA&lt;/option&gt;
                  &lt;option value="21"&gt;KAELE&lt;/option&gt;
                  &lt;option value="22"&gt;KOUSSERI&lt;/option&gt;
                  &lt;option value="23"&gt;MORA&lt;/option&gt;
                  &lt;option value="24"&gt;YAGOUA&lt;/option&gt;
                  &lt;option value="25"&gt;MOKOLO&lt;/option&gt;
          &lt;/optgroup&gt;
  
          &lt;optgroup label="EST"&gt;
                  &lt;option value="16"&gt;YOKADOUMA&lt;/option&gt;
                  &lt;option value="17"&gt;ABONG-MBANG&lt;/option&gt;
                  &lt;option value="18"&gt;BATOURI&lt;/option&gt;
                  &lt;option value="19"&gt;BERTOUA&lt;/option&gt;
                  &lt;option value="62"&gt;NGUELEMENDOUKA&lt;/option&gt;
          &lt;/optgroup&gt;
  &lt;/select&gt;
  </code></pre>