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 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
231,269 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".