Skip to content

Instantly share code, notes, and snippets.

@esteinborn
Created March 6, 2014 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esteinborn/9398782 to your computer and use it in GitHub Desktop.
Save esteinborn/9398782 to your computer and use it in GitHub Desktop.
Detect whether its a touch or click even that should be fired. jQuery.
// This checks document to see if touchstart exists
// if it does, then your event is 'touchstart'
// if it doesn't (most desktop browsers) then its 'click'.
var clickEventType = ((document.ontouchstart!==null)?'click':'touchstart');
// Old .bind don't use
$('.foo').bind(clickEventType, function(e){});
//Better yet, use .on() for binding:
$('body').on(clickEventType, '.foo', function(e){});
// You can then trigger using the same event type:
$('.foo').trigger(clickEventType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment