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

147
Visualizações
Textarea dynamic height resize scrolling issue

I have a textarea field which loads data dynamically without a scrollbar. If the content goes too large, the page scrolls up if I try to type at the end of the textarea. Please help.

$(document).ready(function() {
  var data = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32";
  $("#myText").val(data);
  
  var textAreaAutoHgt = function(parentId, elementId) {
    console.log(elementId);
    $(parentId).on('change keyup keydown paste cut', elementId, function(e) {
      $(this).height("auto").height(this.scrollHeight + "px");
    }).find('textarea').change();
  };

  textAreaAutoHgt("#container", "#myText");
});
#myText {
  resize: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="container">
  <textarea id="myText">Hello!</textarea>
</div>

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

0

The issue is because you're listening to so many events, and as several of them get fired when a single key is pressed there's a conflict in the logic being run.

To fix the problem, just use the input event. This fires under all the same events you listed and avoids the conflicts:

$(document).ready(function() {
  var data = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32";
  $("#myText").val(data);
  
  var textAreaAutoHgt = function(parentId, elementId) {
    $(parentId).on('input', elementId, function(e) {
      $(this).height("auto").height(this.scrollHeight + "px");
    }).find('textarea').trigger('input');
  };

  textAreaAutoHgt("#container", "#myText");
});
#myText {
  resize: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="container">
  <textarea id="myText">Hello!</textarea>
</div>

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