This page walks you through everything you need to go from zero to your first working text highlight: installing the package, importing it into your project, initialising aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/angezid/advanced-mark.js/llms.txt
Use this file to discover all available pages before exploring further.
Mark instance, calling mark(), and styling the results. By the end you’ll have a solid foundation for exploring the full API.
Install the package
Add advanced-mark.js to your project using your preferred package manager:
If you’re loading the library directly from a
<script> tag without a bundler, download mark.js from the dist/ folder (or use a CDN) and include it in your HTML. Add the charset="utf-8" attribute to ensure correct handling of special characters:Import the library
Import advanced-mark.js using the module format that matches your project setup:For bundlers (Webpack, Vite, Rollup, etc.) or environments that support native ES modules, this is the recommended import style. The package’s
- ES Module
- CommonJS
- Script tag (no bundler)
types field also points to the ES module declaration file, so TypeScript support is automatic.Create a Mark instance
Instantiate
Mark by passing a context — the DOM region you want to search inside. The constructor accepts several types:| Context type | Example |
|---|---|
| Single element | document.querySelector('article') |
| NodeList | document.querySelectorAll('.content') |
| Array of elements | [el1, el2, el3] |
| Selector string | 'article, .post-body' |
When passing an array of elements, advanced-mark.js automatically sorts them by their position in the document, so range-based operations remain accurate regardless of the order you pass them in.
Call mark() to highlight text
Once you have an instance, call Pass an array to highlight multiple terms in one call:Always call
mark() with the term (or array of terms) you want to highlight:unmark() before re-running mark() to avoid stacking duplicate highlight elements:Complete Working Example
Here is a self-contained example you can drop into any HTML file:separateWordSearch: true option splits the string 'lorem ipsum' into individual words and highlights each one independently. The done callback receives three arguments: totalMarks (number of mark elements created), totalMatches (number of text matches found), and termStats (a per-term match count object).
jQuery Usage
If your project already uses jQuery, load the jQuery plugin build and callmark() directly on a jQuery selection:
- ES Module (jQuery)
- Script tag (jQuery)
mark, markRegExp, markRanges, unmark) on every jQuery element:
Next Steps
Explore the individual API references to learn about every available option:mark()
Highlight plain-text search terms with accuracy modes, diacritics, synonyms,
wildcards, and more.
markRegExp()
Highlight regular expression matches, with support for named groups and
cross-element patterns.
markRanges()
Highlight arbitrary character or line ranges specified as start/length pairs.
unmark()
Remove highlights and restore the original DOM text node structure.