Expand all

jquery.highlightText

Highlight matched word partials in a given element.

Source:
To do:
  • Add a function for restoring the previous text.
  • Accept mappings for converting shortcuts like WP: to Wikipedia:.

Methods

$.fn.highlightText(matchString, [options]) → {jQuery}static #

Highlight certain text in current nodes (by wrapping it in <span class="highlight">...</span>).

To use this jQuery plugin, load the jquery.highlightText module with mw.loader.

Example

mw.loader.using( 'jquery.highlightText' ).then( () => {
      // no styling is provided by default.
      mw.util.addCSS( `.highlight { background: yellow; }` )
      $( '#bodyContent' ).highlightText( 'bear' );
} );

Parameters:

Name Type Attributes Description
matchString string

String to match

options Object optional
Properties:
Name Type Attributes Default Description
method string optional
'splitAndHighlight'

Method of matching to use, one of:

  • 'splitAndHighlight': Split matchString on spaces, then match each word separately.
  • 'prefixHighlight': Match matchString at the beginning of text only.
  • 'prefixPlusComboHighlight': Match matchString plus any combining characters at the beginning of text only.
Source:

Returns:

Type
jQuery
Highlight certain text in current nodes (by wrapping it in <span class="highlight">...</span>).