I’m building a wysiwyg-editor with an editable iframe using document.execCommand(). Now i need to use the "insertHTML" command which works perfectly in Chrome and Firefox but of course it doesn’t work in Internet Explorer:
function run() {
document.getElementById("target").focus();
document.execCommand("insertHTML", false, "<b>ins</b>");
}
<div contenteditable id="target">contenteditable</div>
<button onclick="run()">contenteditable.focus() + document.execCommand("insertHTML", false, "<b>ins</b>")</button>
What is the standard solution to this problem? It’s okay if it only works in IE8 but IE7-support would be nice too.
In IE <= 10 you can use the
pasteHTMLmethod of theTextRangerepresenting the selection:UPDATE
In IE 11,
document.selectionis gone andinsertHTMLis still not supported, so you’ll need something like the following:https://stackoverflow.com/a/6691294/96100