Close modal on the outside click

Hello, Kirupa members.
I need your help.
I have 2 modals on my site ( https://coursuit.netlify.app/ ) You can find them on the right side of the header - “Login modal” (press “Login” button) and “Normal modal” (press “Get Started Button”).

I can close the “Normal modal” on the outside click.
Here is the code:

var btns = document.querySelectorAll(".open-it, .btn-modalclose");
for (var i = 0; i < btns.length; i++) {
    btns[i].addEventListener("click", function (e) {
        let modal = document.querySelector('.modal')
        modal.classList.toggle('show-modal');
        window.addEventListener('click', e =>
        e.target == modal? modal.classList.remove('show-modal') : false)
        e.preventDefault();
    });
}

But, I can’t close the “Login modal” on the outside click.
Here is the code:

const openModalLogin = document.getElementById('open-modallogin');
const modal_login = document.getElementById('modal-login');
const login_close = document.getElementById('login-close');

openModalLogin.addEventListener('click', () =>
   modal_login.classList.add('visible'),
);
login_close.addEventListener('click', () =>
   modal_login.classList.remove('visible'),
);
window.addEventListener('click', e =>
   e.target == modal_login ? modal_login.classList.remove('visible') : false);

Could you, please, take a look and tell me what is wrong with the “Login modal” and how can I fix it. Can’t figure it out, the code looks the same to me.

Thank you very much in advance for your help.

Hi @Helena_G - you’ve run into my personal gripe with the modal capabilities we have natively. Dismissing a modal by clicking outside of the modal area is too difficult to make work.

The main difference is that with Get Started, the modal takes up all available space on the page. This ensures clicking on the empty area catches the appropriate code and closes. Your Login modal only takes up as much space as needed:

If you have a background element on your log-in modal that mimics the same behavior as Get Started where it takes up all available space, your dialog should close appropriately. Hopefully :slight_smile:

Hi, @kirupa Thank you very much for your answer.
I’m not sure I understand how to mimic this behaviour.
Could you, please, give a hint.

Thank you very much in advance :slight_smile:

Is this HTML something you have full control over, is it being generated by a CMS?

@kirupa I have the total control of it.

Apologies for my delay on this. Were you able to get it resolved?

@kirupa No. I have the same issue very often when I create a similar modal.
And each time I can’t figure out how to close it on outside click.
So, I would be very thankful to get a solution.
Thank you very much in advance :slight_smile:

This is going to be a bit hacky, but it hopefully gets you looking in the right direction. The gist is that the top-level element in the modal needs to be 100% of the width. It is this element that detects the click and hides the modal. The contents of the modal are what need to be customized for the type of modal we are dealing with. In this case, the modal needs to be right-aligned.

With all that said, here are the CSS-only changes I made to make this work:

.modal-login.visible {
  display: grid;
  justify-content: end;
  grid-template-rows: auto 100%;
}
.modal-login__main {
  padding: 100px 0 0 28px;
  background-color: #2E36CB;
}
.modal-login__header {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  height: 88px;
  padding: 28px 32px;
  background-color: #2E36CB;
  width: 500px;
}

Here is a video of this in action: