I am using jQuery to insert a DOM node after an element, which has a contenteditable parent. I'd like to focus the cursor on the inserted DOM node, but calling focus()
on the inserted element doesn't seem to work. Here is what I tried.
// HTML
<div class="foo" contenteditable>
<h1>Foo</h1>
</div>
// jQuery
$(document).on('keydown', '.foo', function(e){
if (e.keyCode == 13) {
var anchor = $( document.getSelection().anchorNode.parentElement ),
var el = $("<p></p>");
anchor.after(el);
el.focus();
e.preventDefault();
}
});