Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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
26 changes: 25 additions & 1 deletion src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
'use strict';
document.addEventListener('DOMContentLoaded', function () {
const gallery = document.querySelector('.gallery');
const largeImg = document.querySelector('#largeImg');

if (!gallery || !largeImg) {
return; // or handle the missing elements gracefully
}
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've added the null-guard checks for gallery and largeImg, and placed the guard before attaching the event listener so a listener won't be attached to null.

Minor suggestion: The code checks event.target.tagName === 'IMG' and 'A' which is fine. Consider normalizing tagName via .toLowerCase() if you ever compare in lowercase, but not required here.


// eslint-disable-next-line no-shadow
gallery.addEventListener('click', function (event) {
Comment on lines +2 to +5
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 which ensures elements exist before use. However, there's a potential runtime error: you call gallery.addEventListener(...) without checking that gallery is non-null. If the .gallery element is missing, this will throw. Add a null check (e.g., if (!gallery) return;) before adding the listener.

if (event.target.tagName === 'IMG') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Tag name comparisons should use uppercase (tagName is uppercase) or normalize with toLowerCase(). Currently you compare to 'IMG' and 'A' which is correct here — but be explicit about case handling. If you prefer lowercasing, use event.target.tagName.toLowerCase() === 'img'. No functional change required if you stick with uppercase, but avoid using lowercase strings.

const targetSrc = event.target.src;

largeImg.src = targetSrc;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Robustness note: You set largeImg.src only after verifying largeImg exists via the guard — good. Keep the guard at top-level so no further references to DOM nodes occur before the check.

}

if (event.target.tagName === 'A') {
const imgInsideA = event.target.querySelector('img');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential improvement: When the user clicks an element, event.target may be an inner element (e.g., the ) rather than the itself; currently you handle both cases separately which is correct. However, if you ever need to ensure the closest anchor is used, consider using event.target.closest('a') to find the ancestor link. Not required for this task but can make handling more robust.


if (imgInsideA) {
largeImg.src = imgInsideA.src;
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 handling clicks on an <a>, you correctly query for img inside it and guard with if (imgInsideA). Good — this prevents errors if the inner image is missing. No change needed for lines 13-17.

}
}
});
});
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