JSFiddle - React, Tailwind, and code Playground
HTML
<div id="myDiv" contenteditable="true">
<strong>Test:</strong> This is a <strong>really</strong> good test.
</div>
<button type="button" id="myButton">Add a span</button>
CSS
#myDiv{padding:15px;border:solid 1px #000;}
JavaScript
// Variable to stock the caret position
var caretPosition = 0;
// Event to get caret position on keyup and on blur
$('#myDiv').on('keyup focus', function () {
caretPosition = getCaretPosition(this);
});
// Event to add node when click on button
$('#myButton').on('click', function (e) {
e.preventDefault();
addNode(this, caretPosition, '<span>Node</span>');
});
// Function to get caret position with 1 param : the editable box
var getCaretPosition = function (editableBox) {
var position = 0;
// TO DO : get caret position
return position;
}
// Function to add node with 3 params : the editable box, the caret position and the node to add
var addNode = function (editableBox, caretPosition, nodeToAdd) {
// TO DO : add node in element
}