title: Javascript to click on href link
body: <p>I got this code after searching around on stack overflow and it <em>should</em> work, but for some reason I am getting an error. I am trying to write JS that simulates a user clicking on a specific href link that has a specific ID. </p>
  
  <p>This needs to work in all the major browsers (chrome, FF, IE, Safari)</p>
  
  <p>here is the code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;a id="clickhere" href="http://link.com"&gt;Need to simulate user clicking on this link&lt;/a&gt;
  </code></pre>
  
  <p>JS:</p>
  
  <pre><code>fireOnclick();
  function fireOnclick() {
  var target=document.getElementById("clickhere");
  if(document.dispatchEvent) { // W3C
      var oEvent = document.createEvent( "MouseEvents" );
      oEvent.initMouseEvent("click", true, true,window, 1, 1, 1, 1, 1, false, false, false, false, 0, target[0]);
      target[0].dispatchEvent( oEvent );
      }
  else if(document.fireEvent) { // IE
      target[0].fireEvent("onclick");
      }    
  }
  </code></pre>
  
  <p>Any help is appreciated. Thanks</p>