Converting wrapped text to lines (either for line numbering or to break each line into individual elements) is a question that comes up on the boards quite a bit, and I finally had a need to do so, so here it is (for both MooTools and jQuery – the jQuery version is not tested, so if there’s any issues please leave a comment). This particular incarnation breaks each wrapped line into a distinct new element, but could be modified to just count lines pretty easily.

Both version extend either jQuery or Element, and are called the same way:

The method accepts a single optional argument – ‘tag’ – which will serve as the tag name of the element for each line created. If not present, the function assumes ‘div’.



Here’s a quick POC fiddle: