this is the part of javascript bugged
function addCartClicked (event) {
var button = event.target;
var shopProducts = button.parentElement;
var title = shopProducts.getElementsByClassName("product-name")[0].innerText;
var price = shopProducts.getElementsByClassName("price")[0].innerText;
var productImg = shopProducts.getElementsByClassName("product-image")[0].src;
console.log(title, price, productImg);
}
and this is the HTML part
<section class="projects" id="section">
<center><h1 class="ourpr">OUR PRODUCTS:</h1></center>
<div class="content">
<li>
<div class="project-card">
<img src="C:UsersMSIDocumentsINFOex4 serie htmlTOPGcapolista.png" alt="" class="product- image" style="width: 360px; height: 348px;">
<div class="project-info">
<strong class="project-title"> <a class="product-name">makrouna fil</a> <a class="price">10$ </a><a class="addcart" align="center">Add to Cart</a> </strong> </div>
</div>
</li>
an error always showd up :
Uncaught TypeError: Cannot read properties of undefined (reading ‘src’)
even when i put .innerText
it’s still the same idk why
i tried to replace it with innerText still the same
and i tried to put it without "."
var productImg = shopProducts.getElementsByClassName("product-image")[0])
it just show the price and the name no image shown
2
Answers
Just remove the spaces from the
img
class from:to
Please try by removing the space of classname from
<img>
tag asclass="product-image"
. Don’t add unwanted spaces, it’s not a good practice.