20

How to detect if a click() is a mouse click or triggered by some code?

c69
  • 19,951
  • 7
  • 52
  • 82
clarkk
  • 27,151
  • 72
  • 200
  • 340

3 Answers3

28

Use the which property of the event object. It should be undefined for code-triggered events:

$("#someElem").click(function(e) {
    if(e.which) {
        //Actually clicked
    }
    else {
        //Triggered by code
    }
});

Here's a working example of the above.

Update based on comments

Pressing the Enter key when an input element has focus can trigger the click event. If you want to distinguish between code-triggered clicks and all other clicks (mouse or keyboard triggered) then the above method should work fine.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 2
    `which` can detect keyboard activity but OP want only mouse activity – xkeshav Oct 03 '11 at 13:01
  • 3
    How can an element be clicked by the keyboard? – James Allardice Oct 03 '11 at 13:02
  • 1
    You've never pressed enter to log in? – awm Oct 03 '11 at 13:04
  • @awm - Pressing enter does not trigger the click event. – James Allardice Oct 03 '11 at 13:05
  • 2
    Are you sure? If a button or checkbox has focus, the "enter" key will cause a click, as far as I know. – Pointy Oct 03 '11 at 13:08
  • i think that the correct way is checking for e.originalEvent wich is undefined if the event is triggered programmatically (but maybe i'm wrong) – Nicola Peluchetti Oct 03 '11 at 13:09
  • @james That depends. Pressing enter when you're focused on an input field simply does a submit. Pressing enter when you're focused on a button clicks the button. Conceded, logging in is not the best example. – awm Oct 03 '11 at 13:09
  • 1
    @James Allardice, `click` events can be triggered by various different devices to support user accessibility. If you tab through a page, you can use the `enter` key to trigger a `click` event on links and form controls. It is unfortunate how many developers never take the time to consider users with disabilities or who may be using screen readers to browse the internet. – zzzzBov Oct 03 '11 at 13:11
  • I stand corrected. When an input element has focus, pressing enter can trigger the click event. You learn something new every day :) However, for the OPs case I think the solution in my answer should still suffice - programattic "clicks" will still be distinguishable. – James Allardice Oct 03 '11 at 13:11
  • 1
    @JamesAllardice This doesn't work in IE8. `e.which` is `0` (zero) when you manually click... – Šime Vidas Oct 03 '11 at 13:13
  • 1
    @James as i stated maybe then it's more correct to check if originalEvent is undefined. – Nicola Peluchetti Oct 03 '11 at 13:13
  • @JamesAllardice Could you back up your solution with a source which confirms that mouse events contain a `which` property? According to MDN the `e.which` property is defined for keyboard events, not mouse events, and also it's deprecated and should be avoided (and it's non-standard - not part of the DOM Events model). – Šime Vidas Oct 03 '11 at 13:32
  • 1
    @ŠimeVidas - No, my source was just what I've used in the past. No idea where it came from originally! I'm very tempted to delete this answer. The answer by Nicola seems to be suitable. Although http://api.jquery.com/category/events/event-object/ states that all `event` objects will have a `which` property. – James Allardice Oct 03 '11 at 13:33
  • @James i'm pretty sure that e.which is not standard, i think jQuery normalizes that in it's event object – Nicola Peluchetti Oct 03 '11 at 14:19
  • @NicolaPeluchetti - Yeah, that's correct. That's what I meant by my last comment. – James Allardice Oct 03 '11 at 14:23
  • This doesn't work with $('#someElem')[0].click(); In that case, I think you'll have to check other values in the event (layerX, layerY, offsetX, offsetY, pageX, pageY, screenX, screenY). – NoBrainer Nov 11 '15 at 19:52
14

You should check e.originalEvent:

$("#someElem").click(function(e) {
    if(e.originalEvent === undefined) {
        //triggered 
    }
    else {
        //clicked by user
    }
});
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
5

Wait for some time and use event.isTrusted - its supported by IE9, Opera 12, and Firefox Nightly. Webkit will support it too, most probably.

Currently, there is no guaranteed way to know.

c69
  • 19,951
  • 7
  • 52
  • 82