Time for action – adding touch event handlers

Extending the jQuery library is actually pretty easy. First we wrap our extensions in an immediately invoked function expression and pass the jQuery object into it. This is a best practice to make sure the dollar sign is really mapped to jQuery and not being used by something else. Then we define our extension methods by adding them to jQuery's internal $.fn object:

(function($) { $.fn.touchstart = function(handler) { this.each(function(i, e) { e.addEventListener("touchstart", handler); }); return this; }; $.fn.touchmove = function(handler) { this.each(function(i, e) { e.addEventListener("touchmove", handler); }); return this; }; $.fn.touchend = function(handler) { this.each(function(i, e) { e.addEventListener("touchend", ...

Get HTML5 Web Application Development By Example Beginner's guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.