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

236
Vistas
Get text from dynamically-filled textarea

The issue seems to be simple, but I really don't understand. Why the alert window is blank after I type something in the textarea and press the button?

const source = document.querySelector('textarea#source').textContent;
  
document.querySelector('button').addEventListener('click', function() {
  alert(source);
});
<textarea id="source" rows="25"></textarea>
<button>click me</button>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You need to change two things. First, the property you need is value not textContent.

But we don't just replace it because by declaring

const source = document.querySelector('textarea#source').value;

we're setting source to a string by value not by reference. Which means that we're taking what the value is at that moment and set it.So instead, we just set source to reference the element object itself, since setting variables to objects is done by reference.

const source = document.querySelector('textarea#source') // The textArea element

Then when event triggers, our handler calls source.value:

const source = document.querySelector('textarea#source'); // The textArea element
  
document.querySelector('button').addEventListener('click', function() {
  alert(source.value);
});
<textarea id="source" rows="25"></textarea>
<button>click me</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

const source = document.querySelector('textarea#source').textContent

Above line won't mutate when DOM content changes, because its no longer an object. Its just a string (only object like datatypes can mutate) so, do this intead.

const inp = document.querySelector('#source') //document.getElementById('source')

document.querySelector('button').addEventListener('click', function() {
  console.log(inp.textContent)
});
<div id="source" contenteditable>
  hello
</div>
<hr/>
<button>check</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think its because the value of source is defined when that block of code is loaded while the alert is triggered onclick, try this approach and see if it works

document.querySelector('button').addEventListener('click', function() {
  const source = document.querySelector('textarea#source').textContent;
  alert(source);
});
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