Programatically fire crossbrowser click event with JavaScript


The following function triggers a mouseclick event on the DOM-node you pass to it.
I’ve tested it with Internet Explorer 6, 7, 8 and 9, Firefox 3.x and 11 nightly. Chrome 17-dev, Opera 10 and 11, webkit on iOS 5 iPhone 4, webkit on Android 2.3.4, Safari 4.0.5 on Windows. If you need it on another browser/version/os, please test it and post the result as a comment here.

function fireClick(node){
	if ( document.createEvent ) {
		var evt = document.createEvent('MouseEvents');
		evt.initEvent('click', true, false);
		node.dispatchEvent(evt);	
	} else if( document.createEventObject ) {
		node.fireEvent('onclick') ;	
	} else if (typeof node.onclick == 'function' ) {
		node.onclick();	
	}
}

Example usage:

//assumes <a href="http://www.nonobtrusive.com" id="myLink">Click me</a>
var theNode = document.getElementById('myLink');
theNode.onclick = function(){ alert("You clicked a link with href:" + this.href); };
fireClick(theNode);
  1. #1 by Naddt19 on August 31, 2012 - 8:26 am

    It would be too good if you could give explanation of the code

  2. #2 by gaspricery.com on May 26, 2013 - 9:28 pm

    Woah this blog is excellent i really like reading your articles. Keep up the good work! You understand, lots of persons are looking around for this information, you could help them greatly.

  3. #3 by SIPP property investment on June 20, 2013 - 1:03 am

    ´╗┐Exploring stumbleupon.com I noticed your blog book-marked as:
    Programatically fire crossbrowser click event with JavaScript

  4. #4 by Orion on October 2, 2013 - 11:36 am

    It doesn’t work work on IE8.

  5. #5 by Alysson on December 14, 2013 - 5:54 am

    Tested on IE 11, 10, 9, 8, 7, 6, 5 – Not working on ie5, but who cares. I’ve many customers who browse with internet explorer, and all of them insist on using older versions, im very grateful by the code

  6. #6 by dfsfdfs on December 31, 2013 - 12:36 pm

    dfadsfsdf

  7. #7 by Ankarsrum Assistent Billigt on January 18, 2014 - 1:52 pm

    Just put your cooking pans and frying pans into the proper
    section of you dishwashing machineand let the
    burst of water rinse them so powerfully without any traces of mess and oil left.
    3 cubic feet of interior cooking space and features convection heating on the top and bottom.

    One of the best kitchen machines invented since the toaster, the stand mixer has
    become a standard in every cook’s kitchen.

  8. #8 by sharif on January 27, 2014 - 11:23 pm

    idoesnotwork

  9. #9 by Ankarsrum Assistent Original on March 4, 2014 - 4:36 pm

    Just put your cooking pans and frying pans into the proper section
    of you dishwashing machineand let the burst of water rinse
    them so powerfully without any traces of mess and oil left.
    When you’re making an expensive purchase online, it’s nice to know
    that the company will stand behind its product. Since 1919, they
    have been giving homeowners and professional cooks alike great
    quality.

  10. #10 by seo Luton on June 3, 2014 - 9:14 pm

    Paragraph writing is also a excitement, if you know
    after that you can write otherwise iit iis difficult to write.

  11. #11 by assurance boxers for men on June 7, 2014 - 12:35 pm

    I am regular reader, how are you everybody? This article posted at this
    website is genuinely pleasant.

  12. #12 by family halloween costumes with newborn on September 28, 2014 - 7:26 pm

    magnificent issues altogether, you simply won a new reader.
    What could you suggest in regards to your put up that you made a few days ago?

    Any certain?

  13. #13 by Kelsey on October 2, 2014 - 9:39 am

    Unfortunately, apartment owners (usually large companies own all apartments
    in the buildings) reluctantly rent apartments to immigrants who just arrived to Toronto and have no job nor credit history in Canada.

    A lot of renters favor this scenario mainly because it makes their rental property
    feel a lot more secluded. You will find that many parents
    qualify, including dads and two-parent homes.

  14. #14 by skateboarding on October 3, 2014 - 12:14 am

    Because the admin of this site is working, no doubt very quickly it will be famous, due to its quality
    contents.

  15. #15 by our web site on October 3, 2014 - 5:18 am

    A proxy can be used to obscure your IP for various reasons and uses, if you want to sign up at lots of profiles on websites you will want to camouflage your actual IP or if wanting search Google and Bing you certainly need plenty of elite IPs. NoHandsProxy makes it so simple to leech 1000s of usable proxies with the click of a button and is available for a very tiny one time payment.

  16. #16 by escorts seo on October 10, 2014 - 4:52 pm

    Hi there to every , for the reason that I am genuinely keen of reading this website’s post to be
    updated regularly. It carries fastidious stuff.

  17. #17 by second hand cars japan on October 14, 2014 - 8:33 pm

    What i do not realize is actually how you’re now not really much more well-favored than you may be now.
    You are very intelligent. You realize thus considerably when it comes
    to this subject, produced me for my part believe it from a lot of various angles.
    Its like men and women aren’t interested except it is one thing to do with Woman gaga!
    Your own stuffs nice. All the time care for it up!

  18. #18 by ultimas noticias on December 14, 2014 - 6:17 pm

    Paragraph writing is also a excitement, if yoou know after that you can write if not it is complicated to
    write.

  19. #19 by Birgit on January 30, 2015 - 6:01 pm

    In addition to his other skills, his mushrooms are the cherry on top.

    These findings imply that objective reality does not exist and despite its apparent solidity the universe is a
    phantasm, a gigantic and magnificently detailed hologram.
    No one likes playing with a foul-mouthed troll who just ruins the game.

  20. #20 by Russian Car Crash Compilation January - 2015 on February 2, 2015 - 9:52 am

    Moving ahead in time, the Song Dynasty of Emperors (960-1279 AD) was a period of great advance in all branches of the “arts”, including medicine,
    possibly due to the state sponsorship of scholars and
    the wide spread and availability of “written” materials.
    There is also a slim chance the driver of the Cavalier could have cut off another driver, and made that following
    car unintentionally tap the back corner. 8-inch display, 2 megapixel camera, 524 MB of internal memory
    and TV tuner.

  21. #21 by pole on March 18, 2015 - 10:01 pm

    You ought to take part in a contest for one of
    the best blogs online. I’m going to highly recommend this blog!

  22. #22 by my web page on April 10, 2015 - 1:13 pm

    Ductless Air Conditioning: Why You Should Take into account It?

  23. #23 by Ryan Callahan Authentic Jersey on July 3, 2015 - 6:19 am

    Those people Jonathan Drouin jersey traditions have carried more than to currently. I will still text my pals to determine who they like within the Derby, and I will still look at the occasion. Even though I love seeing guys bash a tiny ball in terms of humanly attainable, I admit the old Property Operate Derby structure

  24. #24 by jasonvargas on July 19, 2015 - 5:47 am

    Just put your cooking pans and frying pans into the proper
    section of you dishwashing machineand let the
    burst of water rinse them so powerfully without any traces of mess and oil left.
    3 cubic feet of interior cooking space and features convection heating on the top and bottom.
    cubsonlinestore.com

  25. #25 by jasonvargas on July 27, 2015 - 11:11 am

Comments are closed.