Created
February 27, 2013 14:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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