Look at this <div>
-Element:
<div id="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque pena­tibus et magnis dis parturient montes, nascetur ridiculus mus.</div>
and this css:
div#text {
font-family: Arial;
font-size: 16px;
width: 200px;
}
When this div is rendered, it is split into many lines and might look like this block:
Lorem ipsum dolor sit amet,
consectetuer adipiscing elit.
Aenean commodo ligula
eget dolor. Aenean massa.
Cum sociis natoque pena-
tibus et magnis dis
parturient montes, nascetur
ridiculus mus.
Is there a way to extract, let's say, the 6th line of this div's content using Javascript? This means, I want a function, to which I pass the value 3
and then it returns »tibus et magnis dis«. Is this possible? If so, how can this be done?
Whatch the soft hyphen (­
) in the word »penatibus« that makes the word be splittet into two parts.
In a similar way, think of the css-property hyphen
:
div#text {
hyphens: auto;
}
This causes the rendering-machine to hyphenate the text without the need of inserting dashes or ­
-entities.
Remember, that there is no newline or <br>
or any other marker in the text that splits the text into lines. It is just the rendering-machines word-wrap-algorithm together with css-styles and the textstring itself that is responsible for which words are written into which line.
This is also the reason why this question is different form this.
This Question is also different from this one because the solutions posted there ignore the influence of hyphenations.
Also think of ligatures (like f followed by i in many fonts is replaced by a new glyph, fi, that is a little bit shorter than a normal f followed by a normal i would be. This may influence word-wrap and hyphenation. But when you put each character of your text into a <span>
-element to be able to read the position of all these elements, then you will not see f and i being replaced by its shorter ligature fi, and so this method does not return correct results in all cases.