-3

Simple question I need these few lines of javascript in jquery. Just trying to close a modal when I select outside of it.

Thank you.

 // When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
 if (event.target == myModal) {
    myModal.style.display = "none";
}
}
Matt
  • 137
  • 2
  • 14

2 Answers2

1

I'm assuming the modal is being clicked "myModal" ... so ...

$("#myModal").on("click",function() {
    $(this).hide();
});
LuvnJesus
  • 631
  • 4
  • 9
0

Assuming you will click on body and and check if the target is modal with Id myModal

$('body').on('click',function(event){
  var getTargetId = event.target.id;
  if(getTargetId== myModal){
     $(this).attr('display', 'none')
   }
})

Assuming you will click on the modal with id myModal

$("#myModal").on("click",function() {
    $(this).attr('display', 'none');
});
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
brk
  • 48,835
  • 10
  • 56
  • 78