Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 1 Solution

Vijayalaxmi vastrad
Vijayalaxmi vastrad
2,789 Points

I have multiple pop ups in index page. but they are not working please tell whats wrong in my code

//HTML <div class="modal-open" data-modal="modal1">Open Model 1</div> <div class="modal-open" data-modal="modal2">Open Model-2</div> <div class="modal-open" data-modal="modal3">Open Model-3</div>

<div class="modal" id="modal1">
    <button class="modal-colse">close</button>
    <h1>MODAL 1</h1>
</div>
<div class="modal" id="modal2">
    <button class="modal-colse">close</button>
    <h1>MODAL 2</h1>
</div>
<div class="modal" id="modal3">
    <button class="modal-colse">close</button>
    <h1>MODAL 3</h1>
</div>

//javascrpt code var modalbtns = document.querySelectorAll('.modal-open'); modalbtns.forEach(function (btn) { btn.onclick = function () { var modal = btn.getAttribute('data-modal'); document.getElementById(modal).style.display = 'block'; }; })

var closebtn = document.querySelectorAll('.modal-close'); closebtn.forEach(function (btn) { closebtn.onclick = function () { var modal = btn.closest('.modal').style.display = 'none'; }; })

1 Answer

Steven Parker
Steven Parker
216,165 Points

There are a couple of issues:

  • spelling "modal-colse" instead of "modal-close"
  • assigning to "closebtn.onclick" instead of "btn.onclick"

And while it doesn't hurt, the creation of the variable "modal" in the close handler isn't necessary as it isn't being used and will always contain "none".