Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textarea Auto Height #13

Open
ionurboz opened this issue Nov 20, 2020 · 0 comments
Open

Textarea Auto Height #13

ionurboz opened this issue Nov 20, 2020 · 0 comments

Comments

@ionurboz
Copy link

ionurboz commented Nov 20, 2020

Textarea Auto Height

You may want to use such a function.

  • Inserts the new line in the correct order each time. Other methods change the time to add new lines as the content gets longer, and they start adding 2 lines after a certain content.
  • Also, in this function, the maximum growth height can be defined.
  • This function also takes into account the border width.

Example

https://codepen.io/ionurboz/pen/RwRexRp

Code

    function textareaAutoHeight(el)
    {
      var elBorderWith = parseInt(getComputedStyle(el).borderWidth) || 0;

      el.style.height = 'auto';

      var elHeight = el.scrollHeight + (elBorderWith * 2);

      var elMaxHeight = parseInt(el.dataset.autoheight || 800);

      el.style.overflowY = 'hidden';

      if(elHeight > elMaxHeight)
      {
        elHeight = elMaxHeight;

        el.style.removeProperty('overflow-y');
      }
      el.style.height = (elHeight) + 'px';
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant