Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get nth line of a text by JavaScript?

Tags:

javascript

Consider a simple html element as

<div id="test">
a long text without line break
</div>

The browser will create lines based on the glyph and font sizes. After text arrangement by the browser (e.g. depending on the window width), how to get the lines of the text by JavaScript?

For example:

  1. How to get the total number of lines
  2. How to get the first line as appeared in the current window?
  3. How to get the nth line?
like image 867
Googlebot Avatar asked Jan 06 '14 14:01

Googlebot


1 Answers

No, there is no API that gives you access to the rendered text after layout has occurred. The only way to approximate this is pretty hacky, i.e. add words into a container one at a time and see when it changes height. See this related question:

detecting line-breaks with jQuery?

Yeah, who'd have thought it, even jQuery doesn't do this! ;-)

like image 103
ColinE Avatar answered Oct 03 '22 06:10

ColinE