Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Last active July 5, 2017 16:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/d5aa124c64e9656ce1b9 to your computer and use it in GitHub Desktop.
Save telekosmos/d5aa124c64e9656ce1b9 to your computer and use it in GitHub Desktop.
Highlighter selected text (based on https://jsfiddle.net/jsp04pd3/; also http://codepen.io/telekosmos/pen/PPQxQQ)
.highlight {
background-color: yellow;
}
<div id="test">
Select any part of <b>this text and</b> then click 'Run'.
</div>
<div id="test2">
Select any part of <b>this text and</b> then click 'Run'.
</div>
<button id="execute">Run</button>
// Previously, https://github.com/timdown/rangy/wiki should be imported/got
var applier = rangy.createClassApplier("highlight");
var highlighter = rangy.createHighlighter();
highlighter.addClassApplier(applier);
document.getElementById('execute').addEventListener('click', function() {
highlighter.removeAllHighlights();
highlighter.highlightSelection("highlight");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment