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

Botos Claudia
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Botos Claudia
Front End Web Development Techdegree Graduate 18,765 Points

After opening and closing modal, it won't open again

Hey I am currently working on the 10th techdegree project and I got stuck at the modal. I wanted to work first on the modal, making sure It is displayed once you click on a div and then close after clicking on X . It works the first time, but after I close the modal and click on the div again it does not appear. I struggled to solve it on my own , but I ended up with a headache. Please help! Any advice is well received. Thanks in advance!

************************HTML***********************

<div id='employee-list' class='clearfix employee-list'> <!-- The 12 EMPLOYEE placed using JS --> </div> <!-- End Employee list with JS -->

<div id="modal" class='clearfix modal-bg'> <div id="window-modal" class='modal-content'> <span class="close-btn">Ɨ</span> <div id="content-modal" class='content-modal'> <!-- Employee info when selected --> </div>

   </div>


        </div> <!-- END of MODAL -->

************************CSS***********************

.modal-bg{

visibility: hidden; background-color: rgba(0,0,0,0.3); /* Black w/ opacity */ border: 1px solid #666666; width: 30%; height: 50%; position:absolute; bottom: 10%; left: 35%; overflow:visible; }

.modal-bg.active{ text-align: center; visibility: visible; }

.modal-content { visibility: hidden;

}

.modal-content.active { visibility: visible; }

.close-btn { color: #696969 ; float: right; font-size: 28px; font-weight: bold; padding: 10px; }

.close-btn:hover, .close-btn:focus { color: #2a2a2a; text-decoration: none; cursor: pointer; }

************************JavaScript***********************

$('.employee-list').on('click', function() { $('.modal-bg, .modal-content').addClass('active'); });

$('.close-btn, .modal-bg').on('click', function() {
    $('.modal-bg, .modal-content').removeClass('active');



});

3 Answers

Steven Parker
Steven Parker
231,007 Points

You have two handlers for a click on the "close" button, one at line 11 and one at line 85. The one at line 11 uses the display property to make the modal vanish, and the one at line 85 uses opacity (via CSS class). But since the revealing mechanism uses opacity/CSS you probably don't want that first one at all.

So delete (or comment out) the code from lines 11-15.

Steven Parker
Steven Parker
231,007 Points

This code by itself seems to work well, I was able to open and close the modal multiple times with no issues.

But there's obviously more code that isn't shown here, perhaps the other code is interfering with the modal functionality.

To facilitate a more thorough analysis you could make a snapshot of your workspace and post the link to it here.