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

225
Vistas
I'm trying to save the content of an input into local storage when a button is clicked. What am I doing wrong?

so I have an input element with the id of "answer" and a button. I've added an event listener for the button and tried to call a function that saves the text inside of the input into local storage. The console is telling me that "innerHTML" is null, but i'm not sure what I'm supposed to do.

<input type="text" id="answer" />
<button id="button">click me</button>





var inputAnswer = document.getElementById("#answer")
var buttonEl = document.getElementById("button")

buttonEl.addEventListener("click", saveToStorage())

function saveToStorage() {
localStorage.setItem("task", inputAnswer.innerHTML);
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

getElementById() expects an ID, no hashtag. Change var inputAnswer = document.getElementById("#answer") to var inputAnswer = document.getElementById("answer") (remove the #).

buttonEl.addEventListener("click", saveToStorage()) invokes the saveToStorage() function immediately, which is not what you want. Instead, pass it as a reference: buttonEl.addEventListener("click", saveToStorage).

about 4 years ago · Juan Pablo Isaza Denunciar

0

<input type="text" id="answer" />
<button id="button">click me</button>

<script type="text/javascript">
  var inputAnswer = document.getElementById("answer");
  var buttonEl = document.getElementById("button");

  buttonEl.addEventListener("click", addAnswerToLocalStorage);

  function addAnswerToLocalStorage() {
    const value = inputAnswer.value;
    localStorage.setItem("answer", value);
  }
</script>

There is mistake in getting the reference of input field. Instead of #answer document.getElementById("#answer") it should be only answer only. Further, I checked it on my local machine and it is working fine.

enter image description here

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