Reputation: 85
When my string have spaces words get wrap in table. But because I wanted to preserve spaces in innerHTML so I replaced it with '\u00A0' .
var newString = aString.replace(/\s/g,'\u00A0');
Now when newString gets render in the Table, It is not wrapping up because I have replaced white spaces with corresponding Unicode character.
getTableRow(rowIndex).cells(i).innerHTML = myRow[i]; //Here You can replace myRow[i] with newString
word-wrap CSS property is not working for me.
In short I have used \u00A0 to prevent consecutive spaces from collapsing to a single space but don’t really want to prevent line breaks.
Upvotes: 0
Views: 2080
Reputation: 85
I have found the solution .
Replace white space with brakable visible Unicode character ie "\u2009". It is working perfectly for me.
newString = aString.replace(/\s/g,'\u2009');
getTableRow(rowIndex).cells(i).innerHTML = newString;
Upvotes: 1
Reputation: 201758
Instead of replacing spaces by no-break spaces (\u00A0
), just set white-space
: pre-wrap on the element in CSS. Browser support is fairly good, including IE 8.
But if you need to cover IE 7 and earlier, too, use your current approach but so that you only replace the second space of any pair of consecutive spaces:
aString.replace(/\s\s/g,'\u00A0 ')
Upvotes: 1
Reputation: 6106
If I understand you correctly, you can insert the following tag to tell the browser that it can wrap the text at a certain point.
<wbr>
For more information on this tag, follow this link: http://www.w3schools.com/html5/tag_wbr.asp
The only snag is that IE does not support this tag yet (as of IE9) as far as I know.
Upvotes: 1