Is there a way to keep execCommand("insertHTML") from removing attributes in chrome? Is there a way to keep execCommand("insertHTML") from removing attributes in chrome? javascript javascript

Is there a way to keep execCommand("insertHTML") from removing attributes in chrome?


In this particular case I would suggest using Range.insertNode instead, which will give you total control of what gets inserted:

function insertHTML() {    var sel, range;    if (window.getSelection && (sel = window.getSelection()).rangeCount) {        range = sel.getRangeAt(0);        range.collapse(true);        var span = document.createElement("span");        span.id = "myId";        span.appendChild( document.createTextNode("hi") );        range.insertNode(span);        // Move the caret immediately after the inserted span        range.setStartAfter(span);        range.collapse(true);        sel.removeAllRanges();        sel.addRange(range);    }}