Take the 2-minute tour ×

Say I have a click handler defined:



How can I, within the function handler, tell whether the event was fired programatically, or by the user?

share|improve this question
How does the program go about firing an event programmatically? –  Clayton Jul 13 '11 at 6:05
Why do you need to distinguish the cases? –  Damien_The_Unbeliever Jul 13 '11 at 6:09
@Clayton: $x.click() or $x.trigger('click'). –  mu is too short Jul 13 '11 at 6:19
I had a situation with an image-slider that automatically scrolled through the images, and did so by triggering clicks on the correct nav item associated with the image. The trouble is, when the user clicks on the nav, the automatic rotation should stop. –  Kasapo Aug 8 '12 at 15:15
This works in latest firefox, chrome and IE9. Why not accept the answer below with the highest votes?? @kevino –  OZZIE Oct 10 '13 at 10:00
show 3 more comments

3 Answers

You could have a look at the event object e. If the event was triggered by a real click, you'll have things like clientX, clientY, pageX, pageY, etc. inside e and they will be numbers; these numbers are related to the mouse position when the click is triggered but they will probably be present even if the click was initiated through the keyboard. If the event was triggered by $x.click() then you won't have the usual position values in e. You could also look at the originalEvent property, that shouldn't be there if the event came from $x.click().

Maybe something like this:

        // Probably a real click.
        // Probably a fake click.

And here's a little sandbox to play with: http://jsfiddle.net/UtzND/

share|improve this answer
That's awesome ... I just thought to myself "maybe jQuery triggered events don't have mouse position information"! w00t! –  Kasapo Aug 8 '12 at 15:14
This can be defeated in browsers that support createEvent. jsfiddle.net/UtzND/26 –  Joe Enzminger Sep 15 '12 at 16:43
@JoeEnzminger yes but why would you defeat your own script so to speak...? –  OZZIE Oct 10 '13 at 10:01
I'm concerned about a third party script generating a click event and my script handling it on the assumption that it was user generated (an actual click). –  Joe Enzminger Oct 11 '13 at 13:35
@JoeEnzminger: All bets are off once your code is in the browser, there's nothing you can do that can't be defeated by a sufficiently clever person. I think the best you can do is check a bunch of things: Is there an originalEvent? Is it the right sort of thing? Are there coordinates anywhere? Are the coordinates consistent? Are the coordinates inside #foo? ... –  mu is too short Oct 11 '13 at 17:01
add comment

You can use an extra parameter like this :

$("#foo").click(function(e, from){
    if(from == null) from = 'User';
    // rest of your code

$('#foo').trigger('click', ['Trigger']);

as statued in the jQuery trigger manual

share|improve this answer
The only problem I see with this is that you can't distinguish between a normal click and $x.click(), the .click() caller has to explicitly tell you that they're faking it. This may or may not be an issue depending on what Kevin Owocki is up to. –  mu is too short Jul 13 '11 at 6:25
@mu : I don't quite understand your difference between a "normal" click (user?) and the $x.click() which is the eventHandler (for user click, which may be "normal") –  Shikiryu Jul 13 '11 at 7:54
The difference is that one comes from the user but the other from software. The fake click may be coming from a plugin or some other code that can't be modified to add the extra arguments. I'm not critizing your solution, just noting some possible holes. –  mu is too short Jul 13 '11 at 8:07
Although mu is correct and this will not work in every situation, it was exactly what I was looking for, thank you! Didn't know I could pass the extra parameter like a var, but that's exactly what I needed to test for "real" clicks in a complicated UI that treats some "real" clicks as programmatic. Thanks for both incredibly useful solutions! –  Phil Dec 1 '12 at 15:28
add comment

DOM Level 3 specifies event.isTrusted. This is only currently supported in IE9+ and Firefox (based on my tests. I've also read (although not thoroughly researched) that it can be overridden in some browsers and is probably not yet 100% ready to actually be trusted (unfortunately).

This is a modified version of @mu's fiddle that works on IE and Firefox.

share|improve this answer
Good point. I don't think anything is going to be 100% solid though. You'd have to mix in some tests to see if isTrusted is supported and not writeable. I think the real answer depends on the why behind this question and we never found out why. –  mu is too short Sep 15 '12 at 16:55
@mu, I came to this question after asking a similar, but more general one. My "why" case is I want to make sure I am only performing an action (a financial transaction, albeit a small one) iff the user took direct affirmative action on the page. –  Joe Enzminger Sep 15 '12 at 16:58
add comment

Your Answer


