I\'m using the below code for wrapping long text, entered by users in a text area for commenting:
function addNewlines(comments) {
var result = \'\';
while (
I believe wrapping a text by CSS is a better solution however there is a link here which may be helpful wrap-text-in-javascript
by the way i remember there is a JQuery plugin for wrapping text google it too.
I use this css code for displaying in Torch browser and it works
I've tried word-wrap:break-word;overflow:ellipsis;
....etc, but didn't work.
#xxx{
width: 750px;
word-break: break-word;
}
Try one of these:
word-wrap:no-wrap;
word-wrap: break-word
It might solve your problem
Hi just apply some css on text area like
style="word-wrap: break-word;"
The ones above only work 99% of the time. This is the only one that works for me 100%: http://locutus.io/php/strings/wordwrap/
After looking for the perfect solution using regex and other implementations. I decided to right my own. It is not perfect however worked nice for my case, maybe it does not work properly when you have all your text in Upper case.
function breakTextNicely(text, limit, breakpoints) {
var parts = text.split(' ');
var lines = [];
text = parts[0];
parts.shift();
while (parts.length > 0) {
var newText = `${text} ${parts[0]}`;
if (newText.length > limit) {
lines.push(`${text}\n`);
breakpoints--;
if (breakpoints === 0) {
lines.push(parts.join(' '));
break;
} else {
text = parts[0];
}
} else {
text = newText;
}
parts.shift();
}
if (lines.length === 0) {
return text;
} else {
return lines.join('');
}
}
var mytext = 'this is my long text that you can break into multiple line sizes';
console.log( breakTextNicely(mytext, 20, 3) );