0

I'm trying to count how many characters are displayed in the first line of a text which is wrapped because the width of the span is limited.

I know that counting the characters of a String can be done with "length", but I'm failing to get the first line.

Example:

#divone {
  width: 80px;
  overflow: hidden;
}
<div id="divone">
  <p id="one">Some long text
    <p>
</div>

Displayed the text appears as first line: "some long" and second line "text"

Expected output from JavaScript:

Some long

Pseudocode I thought to solve it:

  1. Detect which is the last word before the text is wrapped

  2. Look this word up in original text and retrieve a substring

  3. Use length() to count characters of the substring.

double-beep
  • 5,031
  • 17
  • 33
  • 41
lydiaP
  • 123
  • 1
  • 13
  • 1
    Possible duplicate of [How to count number of characters in a line using JavaScript](https://stackoverflow.com/questions/8113874/how-to-count-number-of-characters-in-a-line-using-javascript) – Mohammad Sep 26 '18 at 10:48
  • Can you please explain the purpose of knowing the length of the first line ? Is it to hide the rest of the text, or to process it? – Lucio Sep 26 '18 at 10:48
  • @Lucio , I want to process it - I need to compare different Interfaces which are different in size of the div's and therefore I need a parameter which is "maximal charachters per line displayed on UI" – lydiaP Sep 26 '18 at 10:50
  • @Mohammad, thanks - worked with a few changes on the code! – lydiaP Sep 26 '18 at 11:18

1 Answers1

0

Thanks to comments I found I solution which is approx. what I want:

How to count number of characters in a line using JavaScript

lydiaP
  • 123
  • 1
  • 13