Avoid double click on iPod/iPad/iOS when using jQuery click()

Avoid double click on iPod/iPad/iOS when using jQuery click()

On a recent project, I was using the usual jQuery click() method to trigger an event when a link was clicked. This worked just fine on everything I tested (Mac, Windows, Android) but when it came to an iPhone, a single click wasn’t enough…I had to click twice. Very weird behavior. After some searching and testing, came up with this little snipped to get it going.

Need to trigger off ‘touchstart’ if available in the document. Otherwise, fallback to the standard ‘click’.

// old method
$('a.some-class').click(function() {
  // do some action
});

// new method
var clickHandler = ('ontouchstart' in document.documentElement ? 'touchstart' : 'click');
$('a.some-class').bind(clickHandler, function() {
  // do some action
});

Good Luck!

No Comments Yet.

Leave a comment