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

122
Visualizações
Textarea value not cleared

So my problem is that I have a HTML <textarea> element and a button which should clear the textarea. But for some reason this does not work. What am I doing wrong?

function clear() {
  document.getElementById('output').value = '';
}
<textarea spellcheck="false" rows="8" cols="80" id="output">Lorem Impsum</textarea>
<button onclick="clear()">Clear Output</button>

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

0

clear is a reserve word in JavaScript. Please change the name of your function.

function clearText() {
   document.getElementById('output').value = '';
}
<textarea spellcheck="false" rows="8" cols="80" id="output">Lorem Impsum</textarea>
<button onclick="clearText()">Clear Output</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

You're clearing the HTML property, but the function name is reserved. To clear the element you can do:

function clearOutput() {
  const el = document.getElementById('output');
  
  el.value = '';
}

As a side note, you really shouldn't use the onclick attribute, it's better to bind events using addEventListener.

about 4 years ago · Juan Pablo Isaza Relatório

0

Textarea uses .innerHTML not .value and also clear() name wont work because it is already defined in vanilla javascript so clear1(). The below code should work -

function clear1() {
  document.getElementById('output').innerHTML = '';
}
<textarea spellcheck="false" rows="8" cols="80" id="output">Lorem Impsum</textarea>
<button onclick="clear1()">Clear Output</button>

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