You can use the fact that browsers place the carriage immediately after the inserted image and work from there. This requires DOM Range and selection support, which excludes IE <= 8, but if it is important, you can use a library like my Rangy to fill this gap.
Demo: http://jsfiddle.net/xJkuj/
the code:
function previousNode(node) { var previous = node.previousSibling; if (previous) { node = previous; while (node.hasChildNodes()) { node = node.lastChild; } return node; } var parent = node.parentNode; if (parent && parent.nodeType.hasChildNodes()) { return parent; } return null; } document.execCommand("InsertImage", false, "http://placekitten.com/200/300");
Tim down
source share