0

I'm a beginner with Javascript and I'm stuck at something. Here's the code:

const button = document.querySelector('button')

const popupWrapper = document.querySelector('.popup-wrapper')

const popBox = document.querySelector('.popup-box')

const close = document.querySelector('.popup-close')

Notice: the whole wrapper is displayed to none, and the .popup-box is inside if the .popup-wrapper div (in case you didn't include that already:D)


button.addEventListener('click', () =>{ 

  popupWrapper.style.display = 'block';

});

close.addEventListener('click', () => {

  popupWrapper.style.display = 'none';

});

popupWrapper.addEventListener('click', () => {

  popupWrapper.style.display = 'none';

});

What I want to do is whenever I click anywhere on the wrapper space around the .popup-box itself, the whole wrapper (with the box) gets displayed to none, but when I click on .popup-box itself nothing happens. What do I write?

2
  • Please be aware that you can put the code into a snippet to make it more readable. Click the <> symbol when editing. Commented Jan 12, 2020 at 19:47
  • addEventListener callback takes an event as its argument, so if you want to prevent event's propagation, you can use (event) => event.stopPropagation(); Commented Jan 12, 2020 at 19:50

1 Answer 1

3

addEventListener callback takes an event as it's argument, so if you want to prevent event's propagation (prevents further propagation of the current event in the capturing and bubbling phases), you can use (event) => event.stopPropagation(); like here:

popupBox.addEventListener('click', (event) => {

  event.stopPropagation();

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.