javascript - How to get Chosen and FastClick to work on mobile device -
i'm trying add fastclick site uses chosen jquery plugin selects. fastclick, selection boxes stop responding taps on mobile browsers. can replicated chrome device emulation.
you can test on simple jsfiddle:
<select id="foo"> <option>bar</option> <option>asdf</option> </select> $("#foo").chosen()
steps replicate chome canary:
- load http://fiddle.jshell.net/7ftdo0j3/3/show/
- open developer tools , emulate google nexus 7 or apple ipad 1/2 (others might work well)
- try use select.
the main problem have try use 2 libraries together, both either manipulate or interpret touch event weirdly. wanted use phrase "which both black magic touch events", have none experience in using libraries in question, felt perhaps it's not appropriate ;)
joking aside, solution problem add fastclick's needsclick
class dom elements dynamically created chosen:
$("#test").chosen(); $(".chosen-container *").addclass("needsclick"); fastclick.attach(document.body);
at least in tests emulator seemed work. i'll tell why think works.
i noticed when added needsclick
class div.chosen-container
, direct children, touch opened dropdown , didn't. when added children, touch starts work flawlessly. i'm quite sure happens there:
- chosen creates dynamicly
div
elements, mimic dropdown. - when user touches kind of
div
, original top-most element gets stores in mouse event- eg. if user touches main text of dropdown (which
span
element), element gets stored in event
- eg. if user touches main text of dropdown (which
- fastclick checks
needsclick
element- as none of dynamicly created elements have magic class, touch event gets prevented , click event sent element instead
- this work otherwise, seems chosen not listen click events @ all. can test 1 trying send 1
.chosen-container
jquery:
$(".chosen-container").trigger('click');
it not work. when send mousedown
event instead, dropdown opens:
$(".chosen-container").trigger('mousedown');
this might mean perhaps on touch devices chosen listens touchstart
or touchend
events directly, , mean wouldn't need fastclick chosen dropdowns in first place. unfortunately not have real test device me, can not guarantee kind of happy ending voyage :d
we used chosen in 1 project, , had event-related problems there well. there quite code related event bindings in chosen's source file, have i'm not sure happens there. @ least explanation above made sense in wicked mind. hope helps you, , hope you'll forms working once again.
Comments
Post a Comment