Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda