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

138
Visualizações
Getting value from input using .value is returning ""

I want to get the value inside the input box and display it on the console

<form action="#">
    name : <input type="text" id="name">
</form>
let name = document.getElementById('name').value;
console.log(name);

But it is returning empty in console log. How can I get the value from input box?

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use keyup event on input for when you type in input it will display value in console like below example:

const nameInput = document.getElementById('name');

nameInput.addEventListener("keyup", (e)=> {
  console.log(nameInput.value)
});
<form action="#">
    name : <input type="text" id="name">
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

You are getting a blank value of the input field because you have not added any value attribute in the input field. So you have to add a value attribute in the input field.

let name = document.getElementById('name').value;
console.log(name);
<form action="#">
    name : <input type="text" id="name" value="my custom value">
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

Add a button to initiate form submit

<form action="#" id="form1">
    name : <input type="text" id="name">
    <input type="submit" value="Submit">
</form>

Now add event listener for Submit event

document.getElementById("form1").addEventListener("submit", function(e) {
    // Prevent default behavior to avoid page refresh
    e.preventDefault();
    let name = document.getElementById('name').value;
    console.log(name);
})
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