title: Using pure JavaScript trying to change the default option of select box, but having an uncaught TypeError: Cannot read property '0' of undefined
body: <p>Using vanilla JavaScript I am trying to change the default option of a select box.</p>
  <pre><code>var temp = &quot;Approve&quot;;
  var mySelect = document.querySelector('[id^=&quot;select_leave_action_&quot;]').id;
  
  for(var i, j = 0; i = mySelect.options[j]; j++) {
      if(i.value == temp) {
          mySelect.selectedIndex = j;
          break;
      }
  }
  </code></pre>
  <p>When I execute the above code I get the following error:</p>
  <blockquote>
  <p>Uncaught TypeError: Cannot read property '0' of undefined at :4:39`</p>
  </blockquote>
  <p>What is wrong with my code?</p>
  <p>I tried to use <code>getElementsByClassName</code> instead of <code>querySelector</code>, but still have the same problem.</p>