Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery click event not working in mobile browsers

the jQuery click event does not seem to be firing in mobile browsers.

The HTML is as follows:

<!-- This is the main menu --> <ul class="menu">    <li><a href="/home/">HOME</a></li>    <li class="publications">PUBLICATIONS &amp; PROJECTS</li>    <li><a href="/about/">ABOUT</a></li>    <li><a href="/blog/">BLOG</a></li>    <li><a href="/contact/">CONTACT</a></li>  </ul>    <!-- This is the sub-menu that is to be fired on click -->  <div id="filter_wrapper">    <ul id="portfolioFilter">       <li><a href="/nutrition-related/">Nutrition related</a></li>       <li><a href="/essays/">Essays and Nonfiction</a></li>       <li><a href="/commissioned/">Commissioned works</a></li>       <li><a href="/plays/">Plays and performance</a></li>       <li><a href="/new-projects/">New Projects</a></li>     </ul>   </div> 

This is the jQuery script for mobile:

$(document).ready(function(){    $('.publications').click(function() {        $('#filter_wrapper').show();    });  }); 

When I click the "publications" list item on a mobile browser nothing happens.

You can view the site here: http://www.ruthcrocker.com/

Not sure if there are jQuery mobile specific events.

like image 873
Jonathan Moriarty Avatar asked May 23 '12 15:05

Jonathan Moriarty


1 Answers

I know this is a resolved old topic, but I just answered a similar question, and though my answer could help someone else as it covers other solution options:

Click events work a little differently on touch enabled devices. There is no mouse, so technically there is no click. According to this article - http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html - due to memory limitations, click events are only emulated and dispatched from anchor and input elements. Any other element could use touch events, or have click events manually initialized by adding a handler to the raw html element, for example, to force click events on list items:

$('li').each(function(){     this.onclick = function() {} }); 

Now click will be triggered by li, therefore can be listened by jQuery.


On your case, you could just change the listener to the anchor element as very well put by @mason81, or use a touch event on the li:

$('.menu').on('touchstart', '.publications', function(){     $('#filter_wrapper').show(); }); 

Here is a fiddle with a few experiments - http://jsbin.com/ukalah/9/edit

like image 58
Hugo Silva Avatar answered Oct 11 '22 19:10

Hugo Silva