Question

I am trying to replace some text inside a div using a modal window with a form input but for some reason I can't get it to work on IE11. I've realized that it skips new lines when selecting a piece of text containing a <br>

Here is a working example:

http://jsfiddle.net/8VdE9/3/

The funny thing is that I am using the same code in a different page and it gets the new lines fine, which makes me think that I am missing something.

var isIE11 = !!navigator.userAgent.match(/Trident.*rv\:11\./);

range = window.getSelection().getRangeAt(0);
if(!isIE11){
    text = new XMLSerializer().serializeToString(range.cloneContents());
}
else{
    text = range;
}
text = new String(text);
var textBr = nl2br(text.toString());

function nl2br (str, is_xhtml) {
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

Any help would be very appreciated.

Regards

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top