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

117
Views
input.innerHTML returns an empty string

I write html code this

<input type="text" class="inputstyle" id="text" name="text">

so this is JS code

function addtolist() {
//get input
var textappend = document.getElementById("text").innerHTML;
console.log(textappend);
console.log(typeof (textappend));
console.log(choice)
if (textappend != "") {
    choice.push(textappend); // append text to choice
}
    document.getElementById("list").innerHTML = choice; // output list 
}

And i make button if click button. Button will call addtolist() function and this function will append id="text"(type string) to the choice(brank array). I try input text "Hello World" to <input> but addtolist() function it's append blank string to choice. idk why help me solve it pls. (sorry for my grammar I don't english pro)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

To get the value of a <input type="text" element, use .value rather than .innerHTML.

var textappend = document.getElementById("text").value;
about 4 years ago · Juan Pablo Isaza Report

0

So assuming that you have a h1 inside HTML that contains the text that you want to use, you can just use .value instead of .innerhtml, I would suggest you to use it as your project gets bigger using .innerhtml may get complex so use .value instead.

This should be your JS code:

function addtolist() {
//get input
var textappend = document.getElementById("text").value;
console.log(textappend);
console.log(typeof (textappend));
console.log(choice)
if (textappend != "") {
    choice.push(textappend); // append text to choice
}
    document.getElementById("list").innerHTML = choice; // output list 
}

Hope it helps out!

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!