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

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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