Javascript selected text highlighting prob Javascript selected text highlighting prob javascript javascript

Javascript selected text highlighting prob


See Range.extractContents:

document.getElementById('execute').addEventListener('click', function() {    var range = window.getSelection().getRangeAt(0),        span = document.createElement('span');    span.className = 'highlight';    span.appendChild(range.extractContents());    range.insertNode(span);});
.highlight { background-color: yellow; }
<div id="test">    Select any part of <b>this text and</b> then click 'Run'.</div><button id="execute">Run</button>