36

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.

Jonathan Moriarty
  • 498
  • 2
  • 5
  • 13

9 Answers9

47

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

Hugo Silva
  • 6,748
  • 3
  • 25
  • 42
28

Raminson has a nice answer if you are already (or don't mind) using jQuery Mobile. If you want a different solution, why not just modify your code as follows:

change that LI you're having trouble with to include an A tag and apply the class there instead of the LI

<!-- This is the main menu -->
<ul class="menu">
   <li><a href="/home/">HOME</a></li>
   <li><a href="#" class="publications">PUBLICATIONS &amp; PROJECTS</a></li>
   <li><a href="/about/">ABOUT</a></li>
   <li><a href="/blog/">BLOG</a></li>
   <li><a href="/contact/">CONTACT</a></li>
 </ul>

And your javascript/jquery code... return false to stop bubbling.

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

This should work for what you are trying to do.

Also, I noticed your site opens the other links in new tabs/windows, is that intentional?

mason81
  • 1,730
  • 2
  • 18
  • 33
  • 1
    It works with this method of applying the class to an anchor instead of a list item, but it doesn't always appear on the first tap (or 10th tap at that matter). As for the links opening in new windows/tabs that is one of the rules of the place I work. – Jonathan Moriarty May 23 '12 at 15:31
  • 2
    I got it working. Turns out I was using jQuery 1.5; I upgraded to 1.7 and it fixed it! Thanks much! – Jonathan Moriarty May 23 '12 at 15:37
  • You're welcome. I'm glad you got it working. I had problems with mobile browsers before trying to apply click events to LI elements and this seems to be the easiest way to work around it. – mason81 May 23 '12 at 17:00
  • Thanks @mason81 u just made my day :) – BeingSuman Jun 06 '17 at 05:02
7

You can use jQuery Mobile vclick event:

Normalized event for handling touchend or mouse click events on touch devices.

$(document).ready(function(){
   $('.publications').vclick(function() {
       $('#filter_wrapper').show();
   });
 });
Ram
  • 143,282
  • 16
  • 168
  • 197
  • 1
    should we include the jquery.mobile.js for that? will it create any javascript conflict with the jquery.js file? – Varada Feb 06 '13 at 06:11
  • 1
    @Varada Yes, we should. No, it doesn't make a conflict. – Ram Feb 06 '13 at 16:55
5

I had the same problem and fixed it by adding "mousedown touchstart"

$(document).on("mousedown touchstart", ".className", function() { // your code here });

inested of others

4

JqueryMobile: Important - Use $(document).bind('pageinit'), not $(document).ready():

$(document).bind('pageinit', function(){
   $('.publications').vclick(function() {
       $('#filter_wrapper').show();
   });
});
lmgonzalves
  • 6,518
  • 3
  • 22
  • 41
L.A.
  • 679
  • 7
  • 8
2

A Solution to Touch and Click in jQuery (without jQuery Mobile)

Let the jQuery Mobile site build your download and add it to your page. For a quick test, you can also use the script provided below.

Next, we can rewire all calls to $(…).click() using the following snippet:

<script src=”http://u1.linnk.it/qc8sbw/usr/apps/textsync/upload/jquery-mobile-touch.value.js”></script>

<script>

$.fn.click = function(listener) {

    return this.each(function() {

       var $this = $( this );

       $this.on(‘vclick’, listener);

    });

};
</script>

source

ucefkh
  • 2,509
  • 2
  • 22
  • 18
2

With this solution, you probably can resolve all mobile clicks problems. Use only "click" function, but add z-index:99 to the element in css:

  $("#test").on('click', function(){
    alert("CLICKED!")
  });
#test{   
  z-index:99;
  cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<div id="test">CLICK ME</div>

Credit:https://foundation.zurb.com/forum/posts/3258-buttons-not-clickable-on-iphone

PET
  • 21
  • 2
1

Vohuman's answer lead me to my own implementation:

$(document).on("vclick", ".className", function() {
  // Whatever you want to do
});

Instead of:

$(document).ready(function($) {
  $('.className').click(function(){
    // Whatever you want to do
  });
});

I hope this helps!

Ralph David Abernathy
  • 5,230
  • 11
  • 51
  • 78
0
$('.publications').on('mousedown touchstart', 
   $('#filter_wrapper').show();
});
Fahimeh Ahmadi
  • 813
  • 8
  • 13