Skip to content

Commit

Permalink
refactored logic for thumbnail clicks - event delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasheloper committed Aug 2, 2024
1 parent 84a5096 commit c28be89
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ for (let i = 0; i < imgArr.length; i++) {
newImage.setAttribute('src', `images/${imgArr[i]}`);
newImage.setAttribute('alt', imgAlts[i].altText);
thumbBar.appendChild(newImage);
newImage.addEventListener("click", () => {
displayedImage.setAttribute('src', newImage.getAttribute('src'));
displayedImage.setAttribute('alt', newImage.getAttribute('alt'));
});
}


// Alternatively, you can add one event listener to the thumb bar.

thumbBar.addEventListener("click", (event) => {
displayedImage.setAttribute('src', event.target.getAttribute('src'));
displayedImage.setAttribute('alt', event.target.getAttribute('alt'));
});


/* Wiring up the Darken/Lighten button */
btn.addEventListener("click", () => {
if (btn.getAttribute("class") === "dark") {
Expand All @@ -42,3 +46,4 @@ btn.addEventListener("click", () => {
overlay.style.backgroundColor = "rgb(0 0 0 / 0%)";
}
});

0 comments on commit c28be89

Please sign in to comment.