sometimes document.execCommand is the only thing that works but it is Deprecated. Any ideas?
Inserting HTML or Text into contenteditable elements
- Code: Select all
//Much more reliable
function insertHTMLAtCursor2(text)
{
let selection = window.getSelection();
let range = selection.getRangeAt(0);
range.deleteContents();
document.execCommand('insertHTML', false, text);
//range.pasteHTML(text);
//range.insertNode(node);
for (let position = 0; position != text.length; position++)
{
selection.modify('move', 'right', 'character');
};
}
//Not that reliable
function insertHTMLAtCursor(text)
{
let selection = window.getSelection();
let range = selection.getRangeAt(0);
range.deleteContents();
let node = range.createContextualFragment(text);
range.insertNode(node);
for(let position = 0; position != text.length; position++)
{
selection.modify('move', 'right', 'character');
};
}