Skip to content
Open
Changes from 4 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.

}
}
});
});
Loading