Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
'use strict';
const gallery = document.querySelector('.gallery');
const largeImg = document.querySelector('#largeImg');

// eslint-disable-next-line no-shadow
gallery.addEventListener('click', function (event) {
if (event.target.tagName === 'img') {
const targetSrc = event.target.src;

largeImg.src = targetSrc;
}

if (event.target.tagName === 'a') {
const imgInsideA = event.target.querySelector('img');

largeImg.src = imgInsideA.src;
}
});
4 changes: 2 additions & 2 deletions src/styles/main.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
body {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If document.querySelector fails to find the elements, gallery or largeImg will be null which will cause runtime errors when adding the event listener or setting .src. Consider guarding against missing elements or ensuring the DOM elements exist before running this script (e.g., run after DOMContentLoaded or check for null).

display: flex;
justify-content: center;
align-items: center;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good: you wrapped initialization in DOMContentLoaded and use event delegation. However, there's one remaining risk: if the querySelector calls fail (elements missing), gallery or largeImg could be null and calling gallery.addEventListener or assigning largeImg.src would throw. Add null checks after selecting them and exit early (or guard uses). This ensures robustness if elements are absent.

justify-content: center;
min-height: 100vh;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When comparing tag names, event.target.tagName returns an uppercase string in all browsers; your code compares to lowercase 'img' and 'a'. This will make the conditions never true. Use uppercase checks ('IMG', 'A') or normalize with .toLowerCase() on tagName.


Expand All @@ -17,8 +17,8 @@ li {
width: 450px;
}
&__list {
margin-top: 0;
display: flex;
margin-top: 0;
padding-left: 0;
}
&__img {
Expand Down
Loading