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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)