javascript - modal won't close on mobile -


i'm using js modal (no jquery), since have issues approach...

everything works except on mobile user can't close it.

var modal = document.getelementbyid('mymodal'); window.onclick = function(event) {     if (event.target == modal) {         modal.style.display = "none";     } }; 

it related touch i'm missing...

i tried jquery this:

$(document).ready(function(){     $(modal).on('click touchstart', function() {         modal.style.display = "none";     }); }); 

the problem here if user clicks inside modal, disappear...

what need when user clicks outside, modal should disappear...

any ideas how can solve issue?

thanks.

instead of registering click on target, register on document , check see mouse wasn't inside of target

$(document).on ('mouseup touchstart', function (e) {     var container = $("#mymodal");      if (!container.is(e.target) // if target of click isn't container...         && container.has(e.target).length === 0) // ... nor descendant of container     {         container.hide();     } }); 

Comments