title: Javascript select random option
body: <p>I've got problem with this code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;div class="select"&gt;
       &lt;select name="ipd" id="ipd"&gt;
          &lt;option value="1"&gt;192.168.2.1&lt;/option&gt;
          &lt;option value="2"&gt;192.168.2.2&lt;/option&gt;
          &lt;option value="3"&gt;192.168.2.3&lt;/option&gt;
          &lt;option value="4"&gt;192.168.2.4&lt;/option&gt;
          &lt;option value="5"&gt;192.168.2.5&lt;/option&gt;
       &lt;/select&gt;
      &lt;a href="#" onclick="myfunction()"&gt;Select random&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And JavaScript:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function myfunction() {
      var select = document.getElementById('ipd');
      var items = select.getElementsByTagName('option');
      var index = Math.floor(Math.random() * items.length);
  
      select.selectedIndex = index;
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>When i'm remove function line from JS and made code to run after site load it's work fine. But when i'm trying use it in  it doesn't work.
  Can someone explain me why? 
  I try to little debug this and add alert(index) to the function and it's return correct random index, but it don't select anything. </p>