onClick not working on mobile (touch)

132,660

Solution 1

better to use touchstart event with .on() jQuery method:

$(window).load(function() { // better to use $(document).ready(function(){
    $('.List li').on('click touchstart', function() {
        $('.Div').slideDown('500');
    });
});

And i don't understand why you are using $(window).load() method because it waits for everything on a page to be loaded, this tend to be slow, while you can use $(document).ready() method which does not wait for each element on the page to be loaded first.

Solution 2

you can use instead of click :

$('#whatever').on('touchstart click', function(){ /* do something... */ });
Share:
132,660
Dale
Author by

Dale

Front-end / UI developer based in Durban, South Africa.

Updated on March 03, 2021

Comments

  • Dale
    Dale about 3 years

    Ok, what im trying to do is slide a div down when the user clicks a list item.

    Problem is I am using Selectric https://github.com/lcdsantos/jQuery-Selectric which converts a Select box to an unordered list. So when the user clicks a which the source outputs as a list item I want a div to slide down.

    On mobile safari (iOS7) the selectbox UI is the same as the standard selectbox UI.

    What is the best practice when it comes to onClick for mobile devices?

    Basic jquery:

    $(window).load(function() {
            $('.List li').click(function() {
                $('.Div').slideDown('500');
            });
        });
    

    You can see a working example HERE (Advanced search on the side bar)

    Thanks.

  • landed
    landed almost 8 years
    check that you don't get 2 events triggered with using both click and touchstart though.
  • Jai
    Jai almost 8 years
    @landed i guess touchstart works on touch devices so i don't think there would be any issues with click on touch devices.
  • landed
    landed almost 8 years
    You are probably right but if you are developing with desktop with emulating touch then it could be an issue in your app and then knowing this could be the reason was why i mentioned it. I prefer the small hammer lib now to get mobile event.
  • wick3d
    wick3d over 7 years
    Just a note: This code will trigger twice for a device that has both events fired or in the chrome dev tools, while mobile emulatin the view