0

I have a pop up that opens up so users can reply to messages. now when a user clicks <a> tag it opens the popup, but it doesn't close if they hit anywhere in the body. I tried fixing this by using jquery but it keeps opening and then closing the pop up again immediately after clicking on the <a> tag.

My <a> tag:

<a class="msg-icon2"  onclick="openReplyModal(<?php echo $msg_id ; ?>)">
    <i class="fas fa-reply"></i>
</a>

My jquery:

var msg_id;

function openReplyModal(id) {
  msg_id = id
  $("#reply_modal" + msg_id).fadeIn();
  jQuery(document).ready(function($) {
    jQuery("body").click(function() {

      $("#reply_modal" + msg_id).fadeOut();
    });
  });
}

How can I adjust my code so that when the user clicks the button for the first time it would open the popup and stay opened unless he clicks anywhere in the body?

here is the code i got from the answer below:

`function openReplyModal(id, event) {
  $(".modal").hide(); // close other modal
  msg_id = id
  $("#reply_modal" + msg_id).fadeIn();
  event.preventDefault();
  event.stopPropagation();
  
}

jQuery(document).ready(function($) {
  // click on modal doesn't hide itself
  $("body").on("click", ".modal", function(e) {
    e.stopPropagation();

  });
  // clicl anywhere else hides all modals
  $(window).click(function() {
    $(".modal").fadeOut();
    
  });
});`
  • You shouldn't bind an event handler inside another event handler. Every time you click you're adding another event handler to the modal. – Barmar Aug 11 '20 at 05:17
  • yeah i realize that but i dont know how to do it in the right way @Barmar can you please advise? –  Aug 11 '20 at 05:18

2 Answers2

0
jQuery(document).ready(function ($) {
            function openReplyModal(id) {
                msg_id = id
                $("#reply_modal" + msg_id).addClass('model-open').fadeIn();
            }

            $('body').click(function (e) {
                $('.model-open').removeClass('open').fadeOut();
            });
        });
  • this seems really good, but now its not opening my pop up at all. is there anything i should change in my tag? –  Aug 11 '20 at 05:01
  • 2
    it is giving me this error now: `Uncaught ReferenceError: openReplyModal is not defined at HTMLAnchorElement.onclick ` –  Aug 11 '20 at 05:07
  • 2
    Functions called from HTML `onclick` have to be defined in the global scope, not inside `$(document).ready()`. – Barmar Aug 11 '20 at 05:18
  • so what can i do to adjust this? @Barmar –  Aug 11 '20 at 05:19
  • @techie_28 as barmar said the function should be global not inside $(document).ready(). –  Aug 11 '20 at 05:33
  • then you do not need the `.ready()` method at all.. move it out & place it in a `script` tag right after the `head` tag – techie_28 Aug 11 '20 at 05:52
0

Give all your modals a common class, such as class="reply_modal". Then you can have a general click handler that fades them all out when you click on the body.

The reason that the modal closes immediately is that the click event bubbles out from the <a> to the body, so it closes the modal. I added event.stopPropagation() to the function so it won't bubble out.

Use $(window).click() to detect clicks anywhere in the window outside the element. See How do I detect a click outside an element?.

var msg_id;

function openReplyModal(id, event) {
  $(".reply_modal").hide(); // close other modal
  msg_id = id
  $("#reply_modal" + msg_id).fadeIn();
  event.preventDefault();
  event.stopPropagation();
}

jQuery(document).ready(function($) {
  // click on modal doesn't hide itself
  $("body").on("click", ".reply_modal", function(e) {
    e.stopPropagation();
  });
  // clicl anywhere else hides all modals
  $(window).click(function() {
    $(".reply_modal").fadeOut();
  });
});
.reply_modal {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="msg-icon2" onclick="openReplyModal(1, event)" href="#"> <i class="fas fa-reply">open 1</i></a>
<a class="msg-icon2" onclick="openReplyModal(2, event)" href="#"> <i class="fas fa-reply">open 2</i></a>
<div id="reply_modal1" class="reply_modal">This is modal 1<br>
  <input></div>
<div id="reply_modal2" class="reply_modal">This is modal 2<br>
  <input></div>
Barmar
  • 741,623
  • 53
  • 500
  • 612