JSFiddle - React, Tailwind, and code Playground

HTML

<div contentEditable='true'>
    <div><b>this</b> is a pen</div>
item2<br/>
item3<br/>
</div>
<button id='btn'>click</button>

JavaScript

$("#btn").click(function()
{
    var selection = window.getSelection();
    var range = [];
    range = selection.getRangeAt(0);
    console.log(selection);
    console.log(range);
    
    var startNode = selection.anchorNode;
    var endNode = selection.focusNode;
    console.log(startNode);
    console.log(endNode);
    range.setStart(startNode, 0);
    range.setEnd(endNode, 0);
    selection.addRange(range);
    //selection.modify("extend", "forward", "word");
});