I'm writing a math editor and I have problem with inserting div
tag (field for formulas). I want that cursor move to position after inserted div
(field) and then user have ability to continue typing without replace cursor manually.
function insert(elem)
{
var sel = window.getSelection();
var range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(elem);
}
function field()
{
var innerDiv = document.createElement('div');
innerDiv.contentEditable = 'true';
innerDiv.style.minHeight = fontSize()+'pt';
innerDiv.style.minWidth = fontSize()+'pt';
var div = document.createElement('div');
div.contentEditable = 'false';
div.classList.add('boxed');
div.classList.add('inline');
div.appendChild(innerDiv);
insert(div);
}
function fontSize()
{
var el = document.getElementById("main");
var style = window.getComputedStyle(el, null).getPropertyValue('font-size');
var fontSize = parseFloat(style);
return fontSize;
}
.boxed {
margin: 5px;
border: 2px solid black;
max-width: calc(100% - 10px);
}
.inline {
display: inline-block;
vertical-align: middle;
max-width: calc(100% - 10px);
}
<body id="main" spellcheck="false">
<button onclick="field()">table</button>
<div id = "mainDiv" contenteditable="true"></div>
</body>
<div id = 'mainDiv'> <div>New inserted div </div> |cursor here| </div>