I'm trying to add FastClick to a site that already uses the Chosen jQuery plugin for selects. With FastClick, the selection boxes simply stop responding to taps on mobile browsers. This can also be replicated with Chrome device emulation.
You can test this for yourself on this simple jsFiddle:
<select id="foo">
<option>Bar</option>
<option>Asdf</option>
</select>
$("#foo").chosen()
Steps to replicate with Chome canary:
The main problem you have is that you try to use two libraries together, which both either manipulate or interpret touch event weirdly. I wanted to use the phrase "which both do some black magic with touch events", but as I have almost none experience in using the libraries in question, I felt that perhaps it's not appropriate ;)
Joking aside, the solution for your problem is to add FastClick's needsclick
class to all DOM elements dynamically created by chosen:
$("#test").chosen();
$(".chosen-container *").addClass("needsclick");
FastClick.attach(document.body);
At least in my tests with the emulator this seemed to work. I'll tell you why I think it works.
I noticed when I added the needsclick
class only to div.chosen-container
and to its direct children, that sometimes a touch opened the dropdown and sometimes it didn't. When added to all children, touch starts to work flawlessly. I'm quite sure something like this happens there:
div
elements, which mimic a dropdown.div
, the original top-most element gets stores in the mouse event
span
element), that element gets stored in the eventneedsclick
only from this element
.chosen-container
with jQuery:$(".chosen-container").trigger('click');
It does not work. But when you send a mousedown
event instead, dropdown opens:
$(".chosen-container").trigger('mousedown');
This might mean that perhaps on touch devices Chosen already listens touchstart
or touchend
events directly, and that would mean you wouldn't need FastClick with Chosen dropdowns even in the first place. Unfortunately I do not have a real test device now with me, so I can not guarantee this kind of happy ending for your voyage :D
We used Chosen in one project, and we had some event-related problems there as well. There is quite a much code related to event bindings in Chosen's source file, so I have to say I'm not completely sure what really happens there. But at least the explanation above made some sense in my wicked mind. Hope it helps you, and hope that you'll get your forms working once again.