javascript - Editable Div Caret Position -
i have editable div , using button insert image div. right now, doing document.getelementbyid('elementid').innerhtml. += ; in order image added end of div. enter image caret is. how go doing this?
thanks
to insert element @ caret not hard. following function inserts node @ caret (or @ end of selection, if content selected) in major browsers:
function insertnodeafterselection(node) { var sel, range, html; if (window.getselection) { sel = window.getselection(); if (sel.getrangeat && sel.rangecount) { range = sel.getrangeat(0); range.collapse(false); range.insertnode(node); } } else if (document.selection && document.selection.createrange) { range = document.selection.createrange(); range.collapse(false); html = (node.nodetype == 3) ? node.data : node.outerhtml; range.pastehtml(html); } }
Comments
Post a Comment