Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A little plugin to search for text in the DOM
JavaScript
Branch: master

README.md

jQuery Highlight

Find some text in the DOM. The difference between this and other Plugins is that it looks for text in multiple nodes. It uses DOMRanges and therefor it doesn't works in IE < 10.

Add jquery.ba-each2.js to improve the speed.

Usage

You can find some examples here.

Basic usage:

$('body').highlight("Search Query");

This will search for the text "Search Query" in the whole document will only highlight the first hit.

Multiple occurrences:

To highlight all occurrences of the text you have to pass an option object like this:

$('body').highlight("Search Query", {onlyFirst : false});

The 'options'

The default options are:

{
    onlyFirst : true,
    fuzzy : true,
    ignorePrevFounds : true,
    classCountPrefix : '_',
    className : 'jQuery_Highlight',
    ignoredChars : /\r|\n|\s/,
    ignoredTags : /(script|style|iframe|object|embed)/i,
    callback : function () {}
}

onlyFirst : true if only the first occurrence should be highlighted.
fuzzy : Switch to enable/disable fuzzy logic. Default is true.
ignorePrevFounds : don't search in previously found text.
classCountPrefix : the highlighted text will be wrapped in span tags with a class like className + ' ' + classCountPrefix + Upcounting Number
className : the className of all highlighted text
ignoredChars : RegEx to test if the Char is ignored in the comparison.
ignoredTags : RegEx, tags that are ignored while iterating the DOM.
callback : a callback for each highlighted text, to add Custome data and do some fancy stuff with highlighted text.

In Action

I used it in combination with my node.js Quote.fm API wrapper nodequote to create a bookmarklet to highlight the 'topquote' of an article. Just try it

Something went wrong with that request. Please try again.