Posted By

ginoplusio on 11/18/09


Tagged

javascript event click events onclick


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

shapeshifta


How do I click a link with javascript?


 / Published in: JavaScript
 

URL: http://www.barattalo.it/2009/11/18/click-links-with-javascript/

Let you fire click events on any html element.

  1. HTMLElement.prototype.click = function() {
  2. if (document.createEvent) {
  3. var evt = this.ownerDocument.createEvent('MouseEvents');
  4. evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
  5. this.dispatchEvent(evt);
  6. } else if (this.fireEvent) {
  7. this.fireEvent("onclick");
  8. }
  9. }
  10.  
  11. // usage
  12. // <a href="http://www.barattalo.it" id="linktoclick">auto click</a>
  13. document.getElementById("linktoclick").click();

Report this snippet  

You need to login to post a comment.