Skip to content
Open
Changes from all 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
15 changes: 15 additions & 0 deletions src/scripts/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
'use strict';

const thumbnails = document.querySelector('.gallery__list');
const largeImage = document.querySelector('.gallery__large-img');

thumbnails.addEventListener('click', (evt) => {
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 thumbnails is null, thumbnails.addEventListener(...) will throw. Add a guard (e.g. if (!thumbnails || !largeImage) return;) before attaching the listener so the script is safe if the DOM structure changes or the elements are missing.

const currentThumbnail = evt.target.closest('.list-item__link');
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You're using a class selector in closest(...). Consider using event.target.closest('a') (or verifying the found element is an anchor) so the handler doesn't depend on a specific CSS class and correctly matches anchor elements as required by the task.


if (!currentThumbnail) {
return;
}

evt.preventDefault();

largeImage.src = currentThumbnail.href;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using currentThumbnail.href returns an absolute URL; to preserve the original (relative) href value use currentThumbnail.getAttribute('href') when assigning largeImage.src.

});
Loading