jquery - Hide div when clicking outside of it doesn't work on iPad -


i have following code:

$(document).on('click touch', function(e) {     var nav    = $(e.target).closest('.nav-js-trigger');     var hidden = $(e.target).closest('.show-nav')      if ( nav.length > 0 ) {         $('.show-nav').not( nav.next().toggleclass('show-nav') ).removeclass('show-nav');     } else if ( hidden.length === 0 ) {         $('.show-nav').removeclass('show-nav');     } }); 

it hides menu div if user clicks outside of it. test this:

  1. click on menu link.
  2. click outside of yellow div.

the div should hidden.

the problem this not work on ipad. why?

here fiddle.

i have added click touch , tried touchend , touchstart.

got message:

comments may edited 5 minutes

so i'll post answer instead.

  • i suggest using touchend in tests, touch event not exist. maybe tests failed because of browser cache?
  • have tried putting listener on document.documentelement, document.body and/or window?
  • try vanilla javascript like suggested apple:

    document.documentelement.addeventlistener("touchend", function(e){ alert('hello world'); }, false); 

fiddle document.documentelement , touchend


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -