Scroll a textarea to a given line with jQuery

Updated . Posted . Visible to the public.

You can use this code:

function scrollToLine($textarea, lineNumber) {
  var lineHeight = parseInt($textarea.css('line-height'));
  $textarea.scrollTop(lineNumber * lineHeight);      
}

Some caveats about this code:

  1. Your textarea needs to have a line-height in Pixels.
  2. The code will scroll to the line number in the text area, not the line number of the original text. These will differ if any lines wrap at the edge of the textarea.

Also see our solution for scrolling a textarea to a given position with jQuery, which might work better for you. Also some other people have done crazy things Show archive.org snapshot .

Henning Koch
Last edit
Henning Koch
Keywords
Javascript
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-05-02 22:36)