Looking for an idea on how to measure the number of rows

I want to take text on a web page and measure its height in lines, regardless of any other parameter, so if I reduce the width of the text and get more lines, I will get a new line count.

Any ideas how to do this using JS and jQuery?

Also, what line numbering methods are common?

+3
source share
3 answers

Almost the same idea as a Ghommey comment, but with line height instead of height.

Spell here: http://jsfiddle.net/k_rma/vbCJm/

+4
source

Here is a little jQuery function:

$.fn.numLines = function() {
    var tmp = $('<div style="visibility:hidden;">x</div>').appendTo(document.body);
    var lines = this.height() / tmp.height();
    tmp.remove();
    return lines;
};

You just call $('#someElement').numLines()to get the number of rows.

Here is a demo: http://jsfiddle.net/ThiefMaster/sHzMP/

+3
source

This will give the total number of lines in a given text.

  text.split("\n").length
0
source

All Articles