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

189
Views
Why does input.value in my javascript always return an empty string?

I am trying to get a list of numbers from an input field in a very simple number pair counting program. I cannot get any value from my input field - no matter what input I give it console logs "". I'm sure it's an easy tweak I'm missing. The problem line is in handleSearch(): const searchList = input.value; countPairs is a function taking an int and an array and returning an int

document.getElementById("app").innerHTML = `
<h1 style="color:indigo">Let's Count Pairs of Socks!</h1>
<h3 style="color:indigo">Enter a list of number socks:</h3>
<div id=Lisa></div>
`;
const input = document.createElement("input");
input.setAttribute("placeholder", "22, 22, 33");
const Lisa = document.getElementById("Lisa");
Lisa.appendChild(input);
const button = document.createElement("button");
button.innerText = "Get number of pairs!";
button.style.color = "indigo";
Lisa.innerHTML += `<br><br>`;
Lisa.appendChild(button);
const results = document.createElement("div");
Lisa.appendChild(results);
const handleSearch = () => {
  const searchList = input.value;
  console.log(input.value);
  const numPairs = countPairs(searchList.length, searchList);
  const title = document.createElement("h3");
  title.innerText = "The number of pairs is.....";
  title.style.color = "indigo";
  const pairs = document.createElement("h4");
  pairs.innerText = numPairs;
  pairs.style.color = "indigo";
  Lisa.innerHTML += `<br><br>`;
  Lisa.appendChild(title);
  Lisa.appendChild(pairs);
};
button.addEventListener("click", handleSearch);
````````````````````````````````````````````````````````````
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you do

someElement.innerHTML +=

This is equivalent to

someElement.innerHTML = someElement.innerHTML + ...

In other words, all the children get re-parsed according to the new HTML that was set.

If you previously created an element with createElement and put it as a child, if you do .innerHTML += on the parent, the created element will no longer be connected to the DOM - instead, a new element with the same HTML markup will be there instead. So, your input.value will be showing you the value of the input that's no longer there.

Change both occurrences of

Lisa.innerHTML += `<br><br>`;

to

Lisa.insertAdjacentHTML('beforeend', `<br><br>`);
about 4 years ago · Juan Pablo Isaza Report
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!