JSFiddle - React, Tailwind, and code Playground
HTML
<input type="button" unselectable onclick="expandSelection();" value="Expand">
<p contenteditable="true" id="test">Hello, this is some test text. Select a word and then press the 'Expand' button.</p>
JavaScript
function expandSelection() {
if (window.getSelection && window.getSelection().modify) {
var sel = window.getSelection();
sel.modify("extend", "forward", "word");
} else if (document.selection && document.selection.type == "Text") {
var range = document.selection.createRange();
range.moveEnd("word", 1);
range.select();
}
document.getElementById("test").focus();
}