Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created May 8, 2013 15:17
Show Gist options
  • Save wpscholar/5541161 to your computer and use it in GitHub Desktop.
Save wpscholar/5541161 to your computer and use it in GitHub Desktop.
jQuery plugin to force a blur event in iOS when a user taps out of an input.
$.fn.iosTapOut = function() {
return this.each( function() {
var $this = $(this);
var canTouch = 'ontouchstart' in document.documentElement;
if( canTouch ) {
$this.data( 'iosTapOut.active', false );
$(document).bind( 'touchstart.iosTapOut', function(e) {
if( $this.data('iosTapOut.active') ) {
if( ! $(e.target).is($this) ) {
$this.data('iosTapOut.active', false);
$this.blur();
}
}
} );
$this.bind( 'touchstart.iosTapOut', function() {
$this.data( 'iosTapOut.active', true );
} );
}
} );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment