DEV Community

Cover image for Make your textareas auto-expand with just 3 lines of JavaScript! πŸ“
Abdullah Al Masud
Abdullah Al Masud

Posted on

Make your textareas auto-expand with just 3 lines of JavaScript! πŸ“

Make your textareas auto-expand with just 3 lines of JavaScript!
β†’ No jQuery βœ…
β†’ Lightweight βœ…
β†’ Smooth UX βœ…

const textarea = document.querySelector("textarea");
textarea.addEventListener("input", () => {
  textarea.style.height = "auto";
  textarea.style.height = textarea.scrollHeight + "px";
});

/* 
Copyright : Abdullah Al Masud
Facebook : Ctrl + Masud
*/
Enter fullscreen mode Exit fullscreen mode

Perfect for forms, chat apps, and notes!

Top comments (0)