I’m building an E-commerce website, my code to add item to cart gives this error
”uncaught TypeError: Cannot read properties of undefined (reading ‘appendChild’)
at HTMLButtonElement.addItem” HOW CAN I SOLVE THIS?
This is my code
“`
const cartContainer = document.getElementsByTagName(“tbody”)[0];
const addToCart = document.getElementsByClassName(‘buy’);
for (var i =0; i< addToCart.length; i++){
addToCart[i].addEventListener(‘click’, addItem)
}
function addItem(event){
let btn = event.target;
let btnParent = btn.parentElement
let itemName = btnParent.children[2].innerText;
let itemImage = btnParent.children[0].src;
let itemPrice = btnParent.children[3].innerText;
let itemContainer = document.createElement(‘tr’).innerHTML = `
<td><img class=”item-image” src=${itemImage} width=”40″ alt=””></td>
<td class = “item-name”>${itemName}</h3>
</td>
<td class=”item-price”><h3>${itemPrice}</h3></td>
<td><input type = ‘number’ class = ‘num’ value = ‘1’></td>
<td class=”total-price”><h3>${itemPrice}</h3></td>
<td><button class=”delete-button” type=”button”>Remove</button></td>
`
cartContainer.appendChild(itemContainer);
}