Skip to content

Instantly share code, notes, and snippets.

@bolmaster2
Created February 27, 2013 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bolmaster2/5048467 to your computer and use it in GitHub Desktop.
Save bolmaster2/5048467 to your computer and use it in GitHub Desktop.
jQuery plugin to make event for both "click" and "touchend" depending on if its a touch device or not
/**
* jQuery plugin to make event for both "click" and "touchend" depending on if its a touch device or not
*/
(function($){
$.fn.touchClick = function(callback) {
var eventType = is_touch_device() ? "touchend" : "click";
this.each(function() {
$(this).bind(eventType, callback);
if (eventType == "touchend") {
$(this).click(function(e) {e.preventDefault();});
}
});
return this;
function is_touch_device() {
return !!('ontouchstart' in window);
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment