Kevin Powell - CommunityKP-C
Kevin Powell - Community3y ago
24 replies
8

innerText is undefined

I am trying to code a very basic shop for a school project and I tried to get the price of a product by using innerText in a function to update the total price in the cart but when I checked in the console I get this error:
caught TypeError: Cannot read properties of undefined (reading 'innerText')

The function code:
function updateTotal() {
  let cartItemContainer = document.getElementsByClassName("cart-table")[0];
  let cartRows = cartItemContainer.getElementsByClassName("cart-row");
  for (let i = 0; i < cartRows.length; i++) {
    let cartRow = cartRows[i];
    let priceElement = cartRow.getElementsByClassName("cart-price")[0];
    let amountElement = cartRow.getElementsByClassName("amount-input")[0];
    let price = priceElement.innerText;
  }
}
Was this page helpful?