I created and added pre
element with contentEditable
attribute. I inserted some text with non-breaking space and tried to get text and split using simple txt.split(/\s/)
, where txt represent string from pre
element. Unfortunately, this split doesn’t work well in IE7 and return wrong splitted data in array when string contains non-breaking spaces.
I solved this problem by using little trick:
var temp = doc.createElement('div'),
str;
temp.innerHTML = txt;
str = temp.innerText || temp.textContent || temp.text;
temp = null;
and then:
var newString = str.split(/\s/);
Variable txt
contains original string from pre
element. After this little trick the split
works well. Cheers!
Leave a Reply