62

I have an element with an onclick method.

I would like to activate that method (or: fake a click on this element) within another function.

Is this possible?

yoavf
  • 20,945
  • 9
  • 37
  • 38

11 Answers11

84

Once you have selected an element you can call click()

document.getElementById('link').click();

see: https://developer.mozilla.org/En/DOM/Element.click

I don't remember if this works on IE, but it should. I don't have a windows machine nearby.

Ben
  • 1,725
  • 1
  • 12
  • 12
62

If you're using JQuery you can do:

$('#elementid').click();
Eduardo Campañó
  • 6,778
  • 4
  • 27
  • 24
8
var clickEvent = new MouseEvent('click', {
  view: window,
  bubbles: true,
  cancelable: true
});
var element = document.getElementById('element-id'); 
var cancelled = !element.dispatchEvent(clickEvent);
if (cancelled) {
  // A handler called preventDefault.
  alert("cancelled");
} else {
  // None of the handlers called preventDefault.
  alert("not cancelled");
}

element.dispatchEvent is supported in all major browsers. The example above is based on an sample simulateClick() function on MDN.

bnp887
  • 5,128
  • 2
  • 26
  • 30
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 1
    I don't see a sample simulateClick() there. Why we have to copy things here, because pages change. – JosephK Jul 07 '17 at 04:09
  • @JosephK - Under 'Example', click the link titled 'Creating and triggering events' and `simulateClick()` function is right there. – Martin James Jul 23 '18 at 13:34
  • Ah, ok so here: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events – JosephK Dec 10 '18 at 07:36
8

I could be misinterpreting your question, but, yes, this is possible. The way that I would go about doing it is this:

var oElement = document.getElementById('elementId');   // get a reference to your element
oElement.onclick = clickHandler; // assign its click function a function reference

function clickHandler() {
    // this function will be called whenever the element is clicked
    // and can also be called from the context of other functions
}

Now, whenever this element is clicked, the code in clickHandler will execute. Similarly, you can execute the same code by calling the function from within the context of other functions (or even assign clickHandler to handle events triggered by other elements)>

Tom
  • 15,527
  • 5
  • 48
  • 62
7

If you're using jQuery, you need to use the .trigger function, so it would be something like:

element.trigger('click');

http://api.jquery.com/trigger/

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
6

Using javascript you can trigger click() and focus() like below example

document.addEventListener("click", function(e) {
  console.log("Clicked On : ",e.toElement);
},true);
document.addEventListener('focus',function(e){
  console.log("Focused On : ",e.srcElement);
},true);

document.querySelector("#button_1").click();
document.querySelector("#input_1").focus();
<input type="button" value="test-button" id="button_1">
<input type="text" value="value 1" id="input_1">
<input type="text" value="value 2" id="input_2">
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
3

This is a perfect example of where you should use a javascript library like Prototype or JQuery to abstract away the cross-browser differences.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
2

just call "onclick"!

here's an example html:

<div id="c" onclick="alert('hello')">Click me!</div>
<div onclick="document.getElementById('c').onclick()">Fake click the previous link!</div>
hasen
  • 161,647
  • 65
  • 194
  • 231
  • 1
    he didn't mention that he wants to fill an "event" object .. just that he wants to call the method. Plus I'm not sure what evernt object are you talking about! – hasen Dec 07 '08 at 12:43
  • All my reading today pointed to using .click(), using .onclick() solved my problem with safari 5.1.7 on win7 not recognizing .click(). chrome, FF, safari on iPad IOS6 all recognized .click(). It drove me nuts! Thanks hasen j PS $('elementid').onclick() worked with the other browsers so there is no need to exception out for Safari 5.1.7. I have no idea about IE since there are so many issues with it I can't get near this code to test it. That's for another day. – Jim Oct 24 '12 at 11:29
2

For IE there is fireEvent() method. Don't know if that works for other browsers.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

I haven't used jQuery, but IIRC, the first method mentioned doesn't trigger the onclick handler.

I'd call the associated onclick method directly, if you're not using the event details.

Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
-3

You can also try getting the element's onclick attribute and then passing into eval. This should work despite the big taboo over eval. I put a sample below

eval(document.getElementById('elementId').getAttribute('onclick'));
cnaut
  • 144
  • 1
  • 12
  • 2
    This works well unless the onclick script references something that isn't imported into the namespace of the script. – ascotan Jun 06 '11 at 15:26