How can I count the number of rows in a div that does not have a \n
or a <br>
at the end of each row? Exemplifying:
var el = document.getElementById('theDiv');
lines = el.innerHTML.replace(/ |^\s+|\s+$/g,'').split('\n'),
lineCount = lines.length;
alert(lineCount); // 5 = correto.
div {
background: #333;
color: #fff;
max-width: 60px;
text-align: center;
}
<div id='theDiv'>
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
</div>
At the end of each line there is \n
, so it's simple to do this count only using a split()
. But the problem is: And when there is no such "boundary" between one line and another?
Consider the case of "break" being made by a rule in CSS that defines a maximum width for div , for example:
var el = document.getElementById('theDiv');
lines = el.innerHTML.replace(/ |^\s+|\s+$/g,'').split('\n'),
lineCount = lines.length;
alert(lineCount); // vai mostrar 1...
div {
background: #333;
color: #fff;
max-width: 60px; /* O css limita a largura e 'quebra' a linha */
text-align: center;
}
<div id='theDiv'>AAAAA BBBBB CCCCC DDDDD EEEEE</div>
The alert()
displayed is not wrong, there really is only one line. But visually speaking, there are 5 lines as in the previous example.
How can I do this line count?
At first I'm looking for an answer without the use of frameworks , but a response using jQuery will certainly help and will be very welcome.