vulcanR
vulcanR

Reputation: 53

jQuery onclick not working on mobile

I'm trying to activate a menu with jQuery with a click (touch) on mobile, but it is not working in mobile. When I do the 'window' resize to try the mobile look, it works with the click, but in an emulator or even trying it with my phone, it doesn't work.

HTML Markup

<img src="i/mobilemenu.jpg" id="mobileMenuButton" style="position:absolute; right:0;"/>

CSS:

#mobileNavigation {display:none}

Javascript Code:

<script type="text/javascript">
            $(document).ready(function(){
                    $('#mobileMenuButton').on('click touchstart',function(){

                            if ($('#mobileNavigation').css('display') == 'none') {
                                $('#mobileNavigation').css('display','block');
                            } 
                            else 
                            {
                                    $('#mobileNavigation').css('display','none'); }
                            });
                    });
                </script>

Upvotes: 5

Views: 9058

Answers (4)

Nachiket Bhosale
Nachiket Bhosale

Reputation: 106

Came across this question and realized the click (and touchstart) should work.

@vulcanR, it is not working in your case is because you already have #mobileNavigation as display: none; So, there is no place for the event to be triggered.

Instead, try the following code and it should work-

$(document).ready(function() {
    $('#mobileMenuButton').on('click touchstart', function() {
        if ($('#mobileNavigation').css('opacity') == '0') {
            $('#mobileNavigation').css('opacity','1');
        } else { 
            $('#mobileNavigation').css('opacity','0'); }
        });
    });
});

The reason behind this working is that opacity:0 retains the height and width of the element whereas display:none makes the dimensions zero, so there is no estate for the event. You could have also used visibility:hidden, but that doesn't listen to click event or any events in general.

Upvotes: 0

optimus
optimus

Reputation: 856

<script type="text/javascript">
   $(document).ready(function(){
      $('#mobileMenuButton').on('mousedown touchstart',function(){
            var userAgent = window.navigator.userAgent;
            if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)||  userAgent.match(/Android/i)) {
         if ($('#mobileNavigation').css('display') == 'none') {
            $('#mobileNavigation').css('display','block');
         } else {
            $('#mobileNavigation').css('display','none'); 
         }
       }
      });
   });
</script>

Just provide the user agent.

Upvotes: 1

elad.chen
elad.chen

Reputation: 2425

Establish a click handler based on the client as such:

var clickHandler = ("ontouchstart" in window ? "touchend" : "click")

and use it whenever you want to listen to click events:

$(".selector").on(clickHandler, function() {...})

This way you can always make sure the proper event is being listened to.

Upvotes: 4

superjinjo
superjinjo

Reputation: 26

I remember when I was building a mobile app, elements that weren't links wouldn't pick up on the click event unless I gave them the CSS property of cursor: pointer. Perhaps this is a similar issue. Try giving the button that property in the style attribute.

Upvotes: 1

Related Questions