I have a popup button in my header. I need to make sure that when clicking outside its zone, the popup closes. How can i do this? In the code I’m trying to add remove active classes when clicking on body.active-search but it doesn’t work.
const body = document.querySelector("body");
const searchButton = document.querySelector(".search-button");
const searchPopup = document.querySelector(".search-popup");
if (searchButton) {
searchButton.addEventListener("click", () => {
searchPopup.classList.toggle("active");
searchButton.classList.toggle("active");
body.classList.toggle("active-search");
});
}
$(".active-search").click(function() {
searchPopup.removeClass("active");
searchButton.removeClass("active");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
<div class="search-wrapper">
<button class="search-button">Open Search</button>
<div class="search-popup"></div>
</div>
</header>
2
Answers
Similar issue was described here Hide popup by clicking elsewhere
Basically you have to add a couple of code lines
Hope this helps.
On BODY "click" use
Event.target.closest("selector")
to determine if a click landed on specific elements selectors, if that’s the case do nothing (return
from the function); otherwise toggle the"active-search"
class on BODY.Also, there’s no need to specifically toggle other active classes on elements. Use CSS instead.
See this closely related answer: https://stackoverflow.com/a/70691308/383904