Domanda

I have some code that inserts html into a contenteditable as follows:

document.execCommand("insertHTML",false, my_html);

Whats the best way to get a reference to the html element that is created?

È stato utile?

Soluzione

Running the command doesn't return you any reference to the inserted content so all you really have available are standard DOM methods. Therefore the easiest way would be to give an ID to the element you want to retrieve. For example:

document.execCommand("insertHTML", false, '<span id="inserted">INSERTED</span>');
var insertedSpan = document.getElementById("inserted");

By the way, the "InsertHTML" command isn't supported in Internet Explorer. You can find cross-browser alternative code here:

https://stackoverflow.com/a/6691294/96100

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top