Created
April 2, 2014 23:22
-
-
Save imjakechapman/9945290 to your computer and use it in GitHub Desktop.
iOS 7 fix for not triggering select's onChange event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.quickChange = function(handler) { | |
return this.each(function() { | |
var self = this; | |
self.qcindex = self.selectedIndex; | |
var interval; | |
function handleChange() { | |
if (self.selectedIndex != self.qcindex) { | |
self.qcindex = self.selectedIndex; | |
handler.apply(self); | |
} | |
} | |
$(self).focus(function() { | |
interval = setInterval(handleChange, 100); | |
}).blur(function() { window.clearInterval(interval); }) | |
.change(handleChange); //also wire the change event in case the interval technique isn't supported (chrome on android) | |
}); | |
}; | |
$('select').quickChange(function(e) { | |
$(this).blur(); | |
$('select').focus(); // prevents an extra blur from firing on focus | |
}); |
I've run across an issue with this where it causes Firefox (both OS X and Windows) to erroneously close the options when the select is triggered. It may be worthwhile restricting this logic to only apply if being viewed on an iOS device (this probably means UA sniffing though, which may be less desirable long-term).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Pleasure to write you,
My name is Favor Williams,
My email address is
( Favor24@live.com)
Am interested to know
more about you,
Contact me for my
photo and other
important issue via,
Favor24@live.com