I'm using the code below, written by Niels Reijnders on this thread Wrap each line of paragraph in a span to wrap each line of a paragraph in span.
function splitLines(container, opentag, closingtag) {
var spans = container.children,
top = 0,
tmp = '';
container.innerHTML = container.textContent.replace(/\S+/g, '<n>$&</n>');
for (let i = 0; i < spans.length; i++) {
var rect = spans[i].getBoundingClientRect().top;
if (top < rect) tmp += closingtag + opentag;
top = rect;
tmp += spans[i].textContent + ' ';
}
container.innerHTML = tmp += closingtag;
}
splitLines(document.querySelectorAll('p')[0], '<span>', '</span>')
It works great and accomplishes what I needed, but only selects the first paragraph of the DOM, not the others. It seems like adding forEach method might solve what I'm looking for, but I don't know how to implement it within this code.
Any help would be very appreciated.
Thanks