How to close a modal by clicking outside it?

Asked

Viewed 5,072 times

0

I made a modal and I need it to be closed by clicking outside of it or the close button. I’ve tried to do with onblur only it doesn’t work, it’s only for tag input. How should I do?

function abre() {
			document.getElementById('modal').style.display = 'block';
}
function fecha() {
			document.getElementById('modal').style.display = 'none';
}
#modal{
			display: none;
			position: fixed;
			z-index: 1;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.5);
		}
		#modalcont{
			width: 400px;
			height: 300px;
			border: 2px solid black;
			position: relative;
			left: 35%;
			top: 20%;
			background-color: lightblue;
			text-align: center;
		}
		#close{
			position: absolute;
			border-radius: 50%;
			font-size: 26px;
			padding: 10px;
			background-color: lightgreen;
			text-align: center;
			right: -10px;
			top: -20px;
			cursor: pointer;
		}
<button onclick="abre()">abra o modal!</button>
	<div id="modal" onblur="fecha()">
		<div id="modalcont">
			<a onclick="fecha()" id="close">X</a>
			<p>teste desse modal</p>
		</div>
	</div>

1 answer

3


I gave similar answers here and here. In this case, it’s even simpler because you have an element overlay where you only need to detect if you hear a click on it with

modal.addEventListener('click', function(e) {
  if (e.target == this) fecha();
});

Example:

var modal = document.getElementById('modal');
modal.addEventListener('click', function(e) {
  if (e.target == this) fecha();
});

function abre() {
  modal.style.display = 'block';
}

function fecha() {
  modal.style.display = 'none';
}
#modal {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

#modalcont {
  width: 400px;
  height: 300px;
  border: 2px solid black;
  position: relative;
  left: 35%;
  top: 20%;
  background-color: lightblue;
  text-align: center;
}

#close {
  position: absolute;
  border-radius: 50%;
  font-size: 26px;
  padding: 10px;
  background-color: lightgreen;
  text-align: center;
  right: -10px;
  top: -20px;
  cursor: pointer;
}
<button onclick="abre()">abra o modal!</button>
<div id="modal" onblur="fecha()">
  <div id="modalcont">
    <a onclick="fecha()" id="close">X</a>
    <p>teste desse modal</p>
  </div>
</div>

  • I didn’t quite understand addeventlistener and target

  • @nelson450 you know what makes the addEventListener?

  • No, and I don’t know the target very well

  • 1

    @nelson450 the addEventListener record in the event that you want to be notified when a given event happens. The event you want to "listen" you pass as the first argument and the function running you pass as the second argument. Does it make sense? (I’ll explain more if you understand)

  • Yes, and the e.target?

  • And onblur only works for forms?

  • @nelson450 ok, so this function always takes an argument, passed by Javascript. This argument is the event, an object, which has the property target. Essa propriedade é o elemento onde o clique aconteceu. Neste caso o addeventlisteneré associado aomodal, então o thisdentro dessa função é **sempre** o modal, mas oe. target` is the element (within the modal or itself) where the click took place. It makes sense?

  • 1

    Okay, I get it now, thank you

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.