What's the cross-browser way to capture all single clicks on a button?

Posted by sany on Stack Overflow See other posts from Stack Overflow or by sany
Published on 2010-04-05T04:54:21Z Indexed on 2010/04/05 8:53 UTC
Read the original article Hit count: 238

What's the best way to execute a function exactly once every time a button is clicked, regardless of click speed and browser?

Simply binding a "click" handler works perfectly in all browsers except IE.

In IE, when the user clicks too fast, only "dblclick" fires, so the "click" handler is never executed. Other browsers trigger both events so it's not a problem for them.

The obvious solution/hack (to me at least) is to attach a dblclick handler in IE that triggers my click handler twice. Another idea is to track clicks myself with mousedown/mouseup, which seems pretty primitive and probably belongs in a framework rather than my application.

So, what's the best/usual/right way of handling this? (pure javascript or jQuery preferred)

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about javascript-events