Highlight text using jQuery and the HTML5 mark tag

Here’s a jQuery plugin I wrote that’ll add HTML5 <mark> tags around keywords or phrases in an element’s body text.

What is mark?

a run of text in one document marked or highlighted for reference purposes, due to its relevance in another context

http://www.whatwg.org/specs/web-apps/

Usage

Add the script to your page, and then when the DOM is ready, do something like:

    $('body').mark('Lorem ipsum');

The plugin can actually be used to wrap any tag around keywords. Simply pass the tag name you want to use as an option:

    $('body').mark({
      text: 'Lorem ipsum',
      tag:  'span',
      cssClass:  'mark'
    });

The above example will wrap span elements around the words lorem and ipsum. Each span element will have the class ‘mark’. View an example implementation.

TODO

  1. Input sanitation on the text to be marked. Currently the text is dumped straight into a regular expression. It needs to be escaped so that any regular expression meta-characters are interpreted as literals.
  2. Similarly, HTML special characters in the input need to be expanded into their HTML entities.
  3. From what I can see, IE doesn’t like <mark> tags, and seems to automatically self close the opening and closing tags inserted into the DOM. WTF? Can anyone shed any light on this problem? For now, you’re going to have to use a <span> with a CSS class

If you’re interested in contributing, get in touch

2,291 thoughts on “Highlight text using jQuery and the HTML5 mark tag

  1. Pingback: party band london

  2. Pingback: click here for more info

  3. Pingback: phoenix marketing agency

  4. Pingback: seo training phoenix

  5. Pingback: phoenix seo service

  6. Pingback: bands for corporate events

  7. Pingback: seo consultant phoenix

  8. Pingback: phoenix seo

  9. Pingback: Homepage

  10. Pingback: this page

  11. Pingback: phoenix local seo

  12. Pingback: social seo phoenix

  13. Pingback: party bands near me

  14. Pingback: party bands for hire

  15. Pingback: resource

  16. Pingback: corporate event bands

  17. Pingback: phoenix seo

  18. Pingback: phoenix seo services

  19. Pingback: hire band uk

  20. Pingback: wedding band london

  21. Pingback: phoenix video marketing

  22. Pingback: phoenix online marketing

  23. Pingback: hire band uk

  24. Pingback: navigate to this site

  25. Pingback: seo agency in phoenix

  26. Pingback: phoenix digital marketing agency

  27. Pingback: phoenix marketing agency

  28. Pingback: wedding band london

  29. Pingback: phoenix seo

  30. Pingback: click this

  31. Pingback: this page

  32. Pingback: wedding band london uk

  33. Pingback: seo company in phoenix arizona

  34. Pingback: phoenix online marketing

  35. Pingback: party bands london

  36. Pingback: phoenix internet marketing

  37. Pingback: digital marketing agency phoenix

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>