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

129
Vistas
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 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