Skip to content Skip to sidebar Skip to footer

Css Flex Div Grow As Child Textarea Grows (no Jquery)

Imagine I have the following I want when content on textarea 'expand' creates vertical overflow for it to expand parent div, not to create vertical scroll.

Solution 1:

I think this cannot be done with only CSS, and since you don't want a jQuery one, so here is a pure JS solution. The idea is to calculate the height (resize) the textarea each time you update the content

var tex = document.querySelector('textarea.expand');

tex.addEventListener('keydown', resize);

functionresize() {
  setTimeout(function() {
    tex.style.height = 'auto'; //needed when you remove content so we reduce the height
    tex.style.height = tex.scrollHeight + 'px';
  }, 0);
}
div {
  padding: 20px;
}
<divstyle="display:flex; background: gold; flex-direction: column;"><textarea>Do NOT expand this</textarea><textareaclass="expand">Expand this baby</textarea></div>

Post a Comment for "Css Flex Div Grow As Child Textarea Grows (no Jquery)"