Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
Update an attribute value on change

i have this:

<p>Qté : ${quantity}</p> <input type="number" class="itemQuantity" name="itemQuantity" min="1" max="100" value="${quantity}">being added dynamically in html for each product of a cart.

I want that input to update inside "p"tag and inside the cart when there a change inside this input. But lets say for now that i want to update inside "p"tag and value when i change the number with arrow or type it. value="${quantity}" --> that code is saying take the quantity for this product (in cart/localstorage) and set it to default.

i want the changed value to be the new default, if you know what i mean. And i also need this to work for all the products, for now it only works with the first item fo the cart.

for now i'm stuck with

const quantityBox = document.querySelector(".itemQuantity");   
      quantityBox.addEventListener("change", function () {
    //update value function   });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If I understood your problem correctly, I think what you want is:

  1. There are many products and each product has 'p' for display quantity and 'input' for change quantity.
  2. When 'input' tag value is changed, matching 'p' tag text should be changed, too.

I think you can use Element.previousElementSibling like this:

const quantityBox = document.querySelector(".itemQuantity");   
quantityBox.addEventListener("change", function (e) {
    const prevP = e.target.previousElementSibling;
    prevP.innerText = "Qté : " + e.target.value;
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda