Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!