Skip to content

Instantly share code, notes, and snippets.

@sstephenson
Created June 22, 2010 17:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sstephenson/448808 to your computer and use it in GitHub Desktop.
Save sstephenson/448808 to your computer and use it in GitHub Desktop.
// Mobile Safari doesn't fire touchstart or touchend events for
// input[type=text], input[type=password], input[type=search],
// and textarea elements.
(function() {
if (!("createTouch" in document)) return;
var selector = "input[type=text], input[type=password], input[type=search], textarea";
function fire(element, type, identifier) {
var touch = document.createTouch(window, element, identifier, 0, 0, 0, 0);
var touches = document.createTouchList(touch);
var targetTouches = document.createTouchList(touch);
var changedTouches = document.createTouchList(touch);
var event = document.createEvent("TouchEvent");
event.initTouchEvent(type, true, true, window, null, 0, 0, 0, 0, false, false, false, false,
touches, targetTouches, changedTouches, 1, 0);
element.dispatchEvent(event);
}
document.on("mouseup", selector, function(event, element) {
var identifier = new Date().getTime();
fire.defer(element, "touchstart", identifier);
fire.defer(element, "touchend", identifier);
});
})();
@madrobby
Copy link

Sweet!

@KrofDrakula
Copy link

Just stumbled upon this snippet while searching for documentation on createTouchList() methods. Do you have an example that would create a TouchList with more than one touch? Can't seem to find any concrete samples for a multitouch simulated event.

@sstephenson
Copy link
Author

Sorry @KrofDrakula, I'm not quite sure how to do that. You might want to take a look at the (rather sparse) Apple documentation here: http://developer.apple.com/library/safari/#documentation/UserExperience/Reference/DocumentAdditionsReference/DocumentAdditions/DocumentAdditions.html

@KrofDrakula
Copy link

Yeah, it's missing exactly what I need to complete my proof of concept. Thanks for replying though. Got any more tips on where to look for more info?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment