Read more

Scroll a textarea to a given line with jQuery

Henning Koch
May 03, 2012Software engineer at makandra GmbH

You can use this code:

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

Opscomplete powered by makandra brand

Save money by migrating from AWS to our fully managed hosting in Germany.

  • Trusted by over 100 customers
  • Ready to use with Ruby, Node.js, PHP
  • Proactive management by operations experts
Read more Show archive.org snapshot

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 .

Posted by Henning Koch to makandra dev (2012-05-03 00:36)