Get highest line number visible in textarea

Hey Sitepoint,

I’m curious if there is a way to get the highest line number being displayed in a textarea.

Lets say for example you have a textarea that is set to 30 rows, yet there is 40 rows (lines) of text in it and you’re viewing the 40th line of text. This means lines 1-10 wouldn’t be visible and the scroll bar would appear. Following me?

Is there a way to know that the 40th line of text is currently visible in the textarea? Strange question I know!

Any ideas are much appreciated.

Three textarea properties will be useful for you here.

  • clientHeight is the visible height of the text area
  • scrollHeight is the total height, including scrollable area
  • scrollTop is the top-left corner of the scrollable area

When scrollTop is 0, you are at the very top.
When scrollTop is (scrollHeight - clientHeight), you are at the very bottom.

1 Like

Thank you. That should get me started.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.