Supposing I have some random block of text in a single line. Like so
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
But for whatever reason (width settings on the containing element, use of text-zoom etc.), on the viewer’s screen it displays as two or more lines.
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
or
Lorem ipsum dolor sit
amet, consectetur
adipiscing elit.
Is there any way to find out via javascript where those line-wraps happen?
$('p').text() and $('p').html() return Lorem ipsum dolor sit amet, consectetur adipiscing elit. regardless of how the text is displayed.
Here’s what I ended up using (feel free to critique and copy for your own nefarious purposes).
First off, when the edit comes in from the user, it’s broken up with
$(editableElement).lineText(userInput).The newline replacement happens because the editing textbox is populated with
$(editableElement).text(), which ignores<br/>tags, but they will still change the height of the following line in the display for typesetting purposes. This was not part of the initial objective, just fairly low-hanging fruit.When I need to pull out formatted text, I call
$(editableElement).getLines(), whereThe end result is a list of hashes, each one containing the content and vertical offset of a single line of text.
If I just want unformatted text,
$(editableElement).text()still returns