Need some help with a little JavaScript DOM Manipulation to separate out a string onto new lines.
I have a single wrapping div around a string...
<div id= "customer-comments">
// Div contains text of comments & no other wrapping HTML tags
Customer comment 1 - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin in ligula neque. Aenean urna nisi, rutrum ac neque eu, aliquet aliquam ligula. Customer comment 2 - Etiam venenatis lacus quam, et imperdiet risus tempor quis. Aenean elit justo, fermentum ac leo vel, bibendum mattis est. Vestibulum sed condimentum mi, id ullamcorper libero
</div>
I am trying to use vanilla js to append a <br> tag after a specific amount (120 characters) to the string. I am using a frontend interface (profound) to load the screen & there is a feature to add onload JavaScript events to specific screens so I am trying some of the vanilla js below.
// Add new lines for customer comments display panel
let comments = document.getElementById('customer-comments').innerText; // grab comment string in div
function newLines(str) {
if (str.length >= 120) {
let result = '';
while (str.length > 0) {
result += str.substring(0, 120) + '<br>';
str = str.substring(120);
}
return result;
}
return str;
}
newLines(comments);
Appreciate the help
Thank you