Last Updated: February 25, 2016
·
1.317K
· kevincennis

Faster clicks on touch devices

On touch devices, click events can take a while to fire - and for users, that often makes your web app feel slow.

Here's a really easy way to mitigate that problem:

var click = !!('ontouchstart' in window) ? 'touchstart' : 'click';

$('#someElement').on(click, function(){...});

Or...

document.addEventListener(click, function(){...}, false);

So, we bind to touchstart for touch devices to make the interactions faster, and fallback to click for non-touch devices.

2 Responses
Add your response

This is beautifully elegant. Well done!

over 1 year ago ·

awesome. just watch out for using it inside scrollable areas :)

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside