80

In IE, I can just call element.click() from JavaScript - how do I accomplish the same task in Firefox? Ideally I'd like to have some JavaScript that would work equally well cross-browser, but if necessary I'll have different per-browser JavaScript for this.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bruce
  • 8,202
  • 6
  • 37
  • 49
  • 1
    This question was also answered [here](http://stackoverflow.com/questions/906486/how-can-i-programmatically-invoke-an-onclick-event-from-a-anchor-tag-while-keep). –  Dec 10 '10 at 20:45

7 Answers7

109

The document.createEvent documentation says that "The createEvent method is deprecated. Use event constructors instead."

So you should use this method instead:

var clickEvent = new MouseEvent("click", {
    "view": window,
    "bubbles": true,
    "cancelable": false
});

and fire it on an element like this:

element.dispatchEvent(clickEvent);

as shown here.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
  • 1
    Excellent solution for Chrome. The code to get the element that then executes `dispatchEvent(clickEvent);` for me was: `var element = document.getElementById("id-tag");` – kbpontius Apr 08 '15 at 03:15
  • Works every time, whereas element.click() works not every time and is not reliable, especially on mobile devices. – Christian Nov 29 '18 at 08:54
  • 1
    does not work for dynamically added elements – Cybernetic Nov 01 '22 at 20:58
  • How about [click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click)? Any drawback? – rlatief Jun 08 '23 at 10:54
  • 2
    Yes, this one: https://stackoverflow.com/questions/809057/how-do-i-programmatically-click-on-an-element-in-javascript/22469115?noredirect=1#comment618935_809108. – Iulian Onofrei Jun 08 '23 at 18:03
31

For firefox links appear to be "special". The only way I was able to get this working was to use the createEvent described here on MDN and call the initMouseEvent function. Even that didn't work completely, I had to manually tell the browser to open a link...

var theEvent = document.createEvent("MouseEvent");
theEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var element = document.getElementById('link');
element.dispatchEvent(theEvent);

while (element)
{
    if (element.tagName == "A" && element.href != "")
    {
        if (element.target == "_blank") { window.open(element.href, element.target); }
        else { document.location = element.href; }
        element = null;
    }
    else
    {
        element = element.parentElement;
    }
}
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
will
  • 3,975
  • 6
  • 33
  • 48
  • 2
    how does it differe from window.open(element.href, element.target) - at my Firefox it works exactly the same, and displays the ugly yellow bar – iirekm Dec 06 '10 at 14:45
  • 1
    If you set the third last parameter of MouseEvent to true (meaning that metaKey (CMD) button was held down when you clicked), this would not open the tab in a background tab, right? Or would it? – Magne Apr 23 '14 at 13:13
25

Using jQuery you can do exactly the same thing, for example:

$("a").click();

Which will "click" all anchors on the page.

Jake McGraw
  • 55,558
  • 10
  • 50
  • 63
12

element.click() is a standard method outlined by the W3C DOM specification. Mozilla's Gecko/Firefox follows the standard and only allows this method to be called on INPUT elements.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • 7
    Understood, but not helpful when I want to programmatically simulate clicks on non-INPUT elements. – Bruce Apr 30 '09 at 21:15
8

Are you trying to actually follow the link or trigger the onclick? You can trigger an onclick with something like this:

var link = document.getElementById(linkId);
link.onclick.call(link);
jiggy
  • 3,828
  • 1
  • 25
  • 40
  • 1
    interesting, I'll give that a try. This is part of a testing harness, so we don't know ahead of time what specific element we are going to be clicking on - it is whatever the test case specifies. – Bruce Apr 30 '09 at 21:16
  • 3
    You don't need to specify a context; since onclick is a property of 'link' the context will already be set appropriately. – James Apr 30 '09 at 21:21
  • 2
    This didn't work in Chrome. An error was thrown in the console saying the method "call" didn't exist. – kbpontius Apr 08 '15 at 03:16
  • 1
    You must have mistyped. call() is part the [Function prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call). It's definitely there. – jiggy Apr 08 '15 at 17:27
8

Here's a cross browser working function (usable for other than click handlers too):

function eventFire(el, etype){
    if (el.fireEvent) {
      el.fireEvent('on' + etype);
    } else {
      var evObj = document.createEvent('Events');
      evObj.initEvent(etype, true, false);
      el.dispatchEvent(evObj);
    }
}
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 1
    I found I needed to use `el[etype]();` on line 3 to get IE to fire the native event (i was testing with a click handler - see http://jsfiddle.net/Pc8qE/) – Lessan Vaezi Aug 04 '11 at 06:44
  • 1
    To get this to work I had to use a input of type submit for firefox and an input of type button for IE. –  Feb 20 '13 at 12:14
1

I used KooiInc's function listed above but I had to use two different input types one 'button' for IE and one 'submit' for FireFox. I am not exactly sure why but it works.

// HTML

<input type="button" id="btnEmailHidden" style="display:none" />
<input type="submit" id="btnEmailHidden2" style="display:none" />

// in JavaScript

var hiddenBtn = document.getElementById("btnEmailHidden");

if (hiddenBtn.fireEvent) {
    hiddenBtn.fireEvent('onclick');
    hiddenBtn[eType]();
}
else {
    // dispatch for firefox + others
    var evObj = document.createEvent('MouseEvent');
    evObj.initEvent(eType, true, true);
    var hiddenBtn2 = document.getElementById("btnEmailHidden2");
    hiddenBtn2.dispatchEvent(evObj);
}

I have search and tried many suggestions but this is what ended up working. If I had some more time I would have liked to investigate why submit works with FF and button with IE but that would be a luxury right now so on to the next problem.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
krm
  • 11
  • 1