Question

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?

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top