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

125
Visualizações
Onclick to add line input text clear - javascript

I struggle with the issue of clearing the text input field when new line is added on click button. When I add number to input field and click on + button I want the added number in first line stays in input field but instead it clears. The solution is maybe very easy but as I am learning javascript right now I cannot find the reason why the field delete my inputs. Here is my code:

function Item(data) { 

Object.assign(this, data)

this.getItemHtml = function () {
    const {itemNo, quantity} = this;

    return `
        <ul class="grid list-item">
            <li class="col-1">${this.itemNo++}</li>
            <li class="col-2"><input type="number" id="item-qty${itemNo}" required></li>
        </ul>
    `;
}}

const itemNo = new Item(iItem.itemData)

function render() { document.getElementById("i-list").innerHTML += itemNo.getItemHtml() }

function addItem() { render() getInput() updateHtmlValue() }

document.getElementById("add-item").addEventListener("click", addItem)

function getInput() { var el = document.querySelector("ul li:nth-child(2) input") 
 if (el) {
    el.addEventListener("change", function (event) {
        let inputValue = event.target.value;

        if (inputValue < 0) {
            inputValue = 0
        }
        
        localStorage.setItem("inputValue", inputValue)
    })
 }

}

function updateHtmlValue() { var input = localStorage.getItem("inputValue") }
about 4 years ago · Juan Pablo Isaza
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