Here is a snippet of my HTML code containing multiple items:
<ul class="catalog">
<li class="catalog_item catalog_item--default-view">
<div class="catalog_item_image">
<img src="/img/01.png" alt="model01" class="catalog_item_icons__big-foto">
</div>
<ul class="catalog_item_icons">
<li class="catalog_item_icons__preview"><img src="/img/01-01.png" alt="01" class="catalog_item_icons__preview--foto-small"></li>
<li class="catalog_item_icons__preview"><img src="/img/01-02.png" alt="02" class="catalog_item_icons__preview--foto-small"></li>
<li class="catalog_item_icons__preview"><img src="/img/01-03.png" alt="03" class="catalog_item_icons__preview--foto-small"></li>
</ul>
In this scenario, I am attempting to switch the source attribute (src) of img.catalog_item_icons__big-foto with that of img.catalog_item_icons__preview--foto-small
This should occur when clicking on the list item (li) element within its parent ul.catalog.
Here's what I have attempted:
$(".catalog_item_icons__preview").each(function () {
$(this).on("click", function() {
console.log('Clicked preview!');
// obtain the fileName of the image
var smallImagePath = $(this).children("img").attr("src");
console.log("small image: " + smallImagePath);
var $bigPreview = $(this).closest('img.catalog_item_icons__big-foto').attr("src");
// log the src of the nearest big image in the console
console.log($bigPreview);
});
});
Although I can retrieve the src of the small image, I encounter issues accessing the src of the image higher up in the hierarchy. Console output: undefined. Troubleshooting why remains puzzling :(