Skip to content

Instantly share code, notes, and snippets.

@yonixw
Last active December 22, 2025 02:00
Show Gist options
  • Select an option

  • Save yonixw/e884162062046eceb0890ff27477a78d to your computer and use it in GitHub Desktop.

Select an option

Save yonixw/e884162062046eceb0890ff27477a78d to your computer and use it in GitHub Desktop.
HIGHLIGHT JS CODE
// https://stackoverflow.com/questions/16251505/how-to-highlight-all-text-occurrences-in-a-html-page-with-javascript
/*
* Search for text in the window ignoring tags
*
* Parameters:
* text: a string to search for
* backgroundColor:
* "yellow" for example, when you would like to highlight the words
* "transparent", when you would like to clear the highlights
* */
function doSearch(text, backgroundColor) {
if (window.find && window.getSelection) {
document.designMode = "on";
var sel = window.getSelection();
sel.collapse(document.body, 0);
while (window.find(text)) {
document.execCommand("HiliteColor", false, backgroundColor);
sel.collapseToEnd();
}
document.designMode = "off";
}
}
doSearch("long first","green")
doSearch("second","pink")
doSearch("first","yellow")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment