Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Modal transparency etc..

HI,

If you look at this pen, the code works, but, the modal is behind it. Z-index is set as well.

Now, did I mess this up? Or I should rather do a div that closes on the same line, and then new div?

Steven Parker
Steven Parker
243,656 Points

Behind what?

The modal appears to be on top. What is the "it" you mean when you say "the modal is behind it"?

Yes, you are right. I was wrong, It confused me before.

Now, what I need to do is:

When click on modal box, don't hide anything.

When click on the 'X', or on the overlay, hide.

I don't know, but usually, is this stuff made like this? Or in OOP? It would be much simpler to think about this as objects, I think.

1 Answer

Steven Parker
Steven Parker
243,656 Points

:point_right: It's a simple matter of how you trigger and then handle the click event.

Right now, you catch any click anywhere on the overlay to remove it. But you can just as easily catch only a click on the close button, and then remove the overlay.

So instead of this:

modalOverlay.addEventListener('click', function() {
  this.parentNode.removeChild(this);
});

Do this:

var modalClose = document.querySelector(".m-modal__closeButton");
modalClose.addEventListener('click', function() {
  modalOverlay.parentNode.removeChild(modalOverlay);
});

So, what about If I want to hide it when I click the overlay, and the close button? Because If i click overlay everything disapears, the code you showed on button will close everything, but what about close when click on overlay, but don't close on modal box?

Steven Parker
Steven Parker
243,656 Points

The code you had to begin with already hides (removes) the overlay when you click either the overlay or the close button, because of propagation (bubbling).

Closing on a click on the overlay but not for a click on the modal could be done by using your original event handler again (along with the new one), and adding a test inside to see if the target is the modal. When it is, just return without doing the remove.