Tell us what’s happening:
Okay so can anyone please tell me why I pass tests 14&15 when I use querySelector but fail when I use querySelectorAll?
The click only works for the first picture but I pass the tests and when I use all the click doesn’t work and i fail them. What is going on!
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lightbox Viewer</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Image Gallery</h1>
<div class="gallery">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/stonehenge-thumbnail.jpg">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/storm-thumbnail.jpg">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/trees-thumbnail.jpg">
</div>
<div class="lightbox">
<button id="close-btn"></button>
<img id="lightbox-image">
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
h1 {
text-align: center;
font-size: 38px;
background-color: lightblue;
}
.lightbox {
background-color: rgba(80,80,80,0.5);
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display: none;
}
.gallery {
display: flex;
height: 150px;
justify-content: space-between;
margin: 50px;
}
img {
}
/* file: script.js */
const lightbox = document.querySelector(".lightbox");
const closeBtn = document.getElementById("close-btn");
const galleryItem = document.querySelector(".gallery-item");
const lightboxImage = document.getElementById("lightbox-image");
const gallery = document.querySelector(".gallery");
const allGalleryItem = document.querySelectorAll(".gallery-item");
const noThumbnail = galleryItem.src.replace("-thumbnail","");
galleryItem.addEventListener("click", () => {
lightbox.style.display = "flex";
galleryItem.src = noThumbnail;
}
);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build a Lightbox Viewer - Build a Lightbox Viewer