Skip to content

Instantly share code, notes, and snippets.

@tonis2
Created April 19, 2018 08:53
Show Gist options
  • Save tonis2/91c24ac8b8b9274665b22a9659d39b8d to your computer and use it in GitHub Desktop.
Save tonis2/91c24ac8b8b9274665b22a9659d39b8d to your computer and use it in GitHub Desktop.
contentEditable insert element and focus behind it.
const container = document.querySelector(".pell-content");
let selection = window.getSelection();
let range = selection.getRangeAt(0);
let dom = HTML`<div id="node"><span style="color:red">${selection.toString()}</span></div>`;
selection.deleteFromDocument();
range.insertNode(dom);
///Insert first node
range.setStartAfter(dom);
range.setEndAfter(dom);
selection.removeAllRanges();
selection.addRange(range);
//Focus behind it.
range.insertNode(HTML`<span>Iöötab raisk</span>`);
//Insert next node.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment