I use the code below to wrap long text entered by users in the text area for comments:
function addNewlines(comments) {
var result = '';
while ($.trim(comments).length > 0) {
result += comments.substring(0,70) + '\n';
comments = comments.substring(70);
}
return result;
}
The problem is shown in the screenshot below. Any ideas on how to solve it? Can we use the method lastindexof(" ")to get the last space in a substring to solve this problem logically? Can someone tweak this little code to make it correct?

source
share