Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Select Text
function selectText(selection) {
var range = selection.getRangeAt(0);
function replaceRange(s, start, end, substitute) {
return s.substring(0, start) + substitute + s.substring(end);
var text = selection.baseNode.textContent.substring(range.startOffset, range.endOffset);
var mark = `<mark>${text}</mark>`;
range.startContainer.parentElement.innerHTML = replaceRange(range.startContainer.textContent, range.startOffset, range.endOffset, mark);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment