O'Reilly logo

HTML5 Web Application Development By Example Beginner's guide by J.M. Gustafson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required