I need to update the innerHTML of a contenteditable P element after each keystroke, in JavaScript
(no jQuery)
I can't use an input or textarea instead of the P element.
It works fine, but the caret always goes back at the beginning of the paragraph when the innerHTML is reset.
I tried to use the solutions of the other SO questions that talk about carets and contenteditable but it doesn't seem to work in my case: I want to put the caret back exactly where it was before the update of innerHTML.
p.oninput=function(){
// Get caret position
c = window.getSelection().
getRangeAt(0).
startOffset;
console.log(c);
// Update innerHTML
p.innerHTML = p.innerHTML.toUpperCase();
// Place caret back
// ???
}
p{ border: 1px dotted red }
<p contenteditable id=p>type here
BTW, It doesn't need to work on IE, but if you have a cross-browser solution, I'll take it too.
Thanks for your help!