javascripthtml-entitiescreatetextnode

createTextNode without htmlentities


Consider the following snippet:

var t = "<span>Hello world</span>";
var range = window.getSelection().getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(t));

How can I avoid replacing t with htmlentities?


Solution

  • By not creating a textnode. Instead use range.pasteHTML.