Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
Why is the '.value' property not working?
function updateCartTotal() {
  var cartItemContainer = document.getElementsByClassName('center-cart-items')[0]
  var cartRows          = cartItemContainer.getElementsByClassName('cart-row')
  for (var i = 0; i < cartRows.length; i++) {
    var cartRow         = cartRows[i]
    var priceElement    = cartRow.getElementsByClassName('cart-price')[0]
    var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]
    var price           = parseFloat(priceElement.innerText.replace('$', ''))
    var quantity        = quantityElement.value
    console.log(price * quantity)
  }
}
<div class="center-cart-items">
<div class="cart-row"> 
  <div class="cart-item cart-column"> 
    <img class="cart-item-image" src="#" width="100" height="100" /> 
    <span class="cart-item-title"> T-shirt</span> 
  </div> 
  <span class="cart-price cart-column"> $29.99</span> 
  <div class="cart-quantity cart-column"> 
    <input class="cart-quantity-input" type="number" value="2" /> 
    <button class="btn-remove" role="button">REMOVE</button>
  </div>   
</div>

When I run the code, I get a source error that says
Cannot read properties of undefined (reading 'value')
Not sure what is going on here, I am new to all of this and following guides to help me build.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The bug occurs before the reference to cartRows. There is no .center-cart-items class in the snippet you gave, and if you delete the reference to it, your code works fine (see snippet).

Check your complete html source, the js reference to assign cartItemContainer is probably the problem.

function updateCartTotal() {
  //var cartItemContainer = document.getElementsByClassName('center-cart-items')[0]
  var cartRows          = document.getElementsByClassName('cart-row')
  for (var i = 0; i < cartRows.length; i++) {
    var cartRow         = cartRows[i]
    var priceElement    = cartRow.getElementsByClassName('cart-price')[0]
    var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]
    var price           = parseFloat(priceElement.innerText.replace('$', ''))
    var quantity        = quantityElement.value
    console.log(price * quantity)
  }
return (price * quantity);
}

console.log(updateCartTotal());
<div class="cart-row"> 
  <div class="cart-item cart-column"> 
    <img class="cart-item-image" src="#" width="100" height="100" /> 
    <span class="cart-item-title"> T-shirt</span> 
  </div> 
  <span class="cart-price cart-column"> $29.99</span> 
  <div class="cart-quantity cart-column"> 
    <input class="cart-quantity-input" type="number" value="2" /> 
    <button class="btn-remove" role="button">REMOVE</button>
  </div>   
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I have recreated your code in codePen and it works fine:

https://codepen.io/SweetChillyPhilly/pen/xxpjyNQ

<div class="center-cart-items">
  <div class="cart-row"> 
    <div class="cart-item cart-column"> 
      <img class="cart-item-image" src="#" width="100" height="100" /> 
      <span class="cart-item-title"> T-shirt</span> 
    </div> 
    <span class="cart-price cart-column"> $29.99</span> 
    <div class="cart-quantity cart-column"> 
      <input class="cart-quantity-input" type="number" value="2" /> 
      <button class="btn-remove" role="button">REMOVE</button>
    </div>   
  </div>
  <div class="cart-row"> 
    <div class="cart-item cart-column"> 
      <img class="cart-item-image" src="#" width="100" height="100" /> 
      <span class="cart-item-title"> T-shirt</span> 
    </div> 
    <span class="cart-price cart-column"> $29.99</span> 
    <div class="cart-quantity cart-column"> 
      <input class="cart-quantity-input" type="number" value="2" /> 
      <button class="btn-remove" role="button">REMOVE</button>
    </div>   
  </div>
  <div class="cart-row"> 
    <div class="cart-item cart-column"> 
      <img class="cart-item-image" src="#" width="100" height="100" /> 
      <span class="cart-item-title"> T-shirt</span> 
    </div> 
    <span class="cart-price cart-column"> $29.99</span> 
    <div class="cart-quantity cart-column"> 
      <input class="cart-quantity-input" type="number" value="2" /> 
      <button class="btn-remove" role="button">REMOVE</button>
    </div>   
  </div>
</div>

JS:

function updateCartTotal() {
  var cartItemContainer = document.getElementsByClassName('center-cart-items')[0]
  var cartRows          = cartItemContainer.getElementsByClassName('cart-row')
  for (var i = 0; i < cartRows.length; i++) {
    var cartRow         = cartRows[i]
    var priceElement    = cartRow.getElementsByClassName('cart-price')[0]
    var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]
    var price           = parseFloat(priceElement.innerText.replace('$', ''))
    var quantity        = quantityElement.value
    console.log(price * quantity)
  }
}

updateCartTotal();

Your console log is working as expected.

So i suspect your issue lies outside of this code.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda