Touch

Adding iOS Touch Events to jQuery Color Picker

iOS Touch Events for Farbtastic Color Picker being tested on the iPad

Today we were working on some JavaScript code for a small HTML5 iPad app. We wanted to use the Farbtastic Color Picker jQuery Plugin to allow users to quickly select from a wide range of colors.

“Farbtastic is a jQuery plug-in that can add one or more color picker widgets into a page through JavaScript. Each widget is then linked to an existing element (e.g. a text field) and will update the element’s value when a color is selected.”
— Quote from the Farbtastic website

Problem…

Unfortunately Farbtastic was not developed with touch events in mind. As usual with any UI code developed for desktop, the mobile user experience is pretty terrible, when it worked. We really liked the Farbtastic Color Picker, so we set out to add iOS Touch Events.

Solved!

The iOS Touch Events are super-easy to work with, so it did not take long to get Farbastic to behave itself appropriately. We ran into a binding issue where the mouse and touch events were clobbering each-other’s callbacks, but that was quickly solved.

Try This Live Demo on Your iPad!

About The Code

We added four functions to the main “fb” object to handle the HTML5 iOS Touch Events:

  1. touchconvert() – converts an iOS touch event into mouse-event friendly data
  2. touchmove() – passes touch events to the existing mouse handler
  3. touchend() – unbinds touch events so default page gestures are not blocked
  4. touchstart() – starts the ball rolling when a touch event begins

Here is a sneak-peek at the code for all the HTML5 JavaScript developers out there:

GIT The Code

If you would like to use the code in your project, go right head! We would love to hear how you used it.

Here are some useful links to get you started:

Leave a Reply