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: live bands for wedding

  2. Pingback: see here

  3. Pingback: internet marketing phoenix

  4. Pingback: phoenix social media marketing

  5. Pingback: live band hire

  6. Get a top phoenix seo experts that is now available in addition with reasonably pricing now only!

  7. Pingback: bands to hire for parties near me

  8. Pingback: moved here

  9. Pingback: more helpful hints

  10. Pingback: wedding music band

  11. Pingback: phoenix marketing solutions

  12. Pingback: local seo phoenix

  13. Pingback: live party band

  14. Pingback: my company

  15. Pingback: find

  16. Pingback: internet marketing phoenix

  17. Pingback: party band

  18. Pingback: social seo phoenix

  19. Pingback: phoenix marketing company

  20. Pingback: uk party band

  21. Pingback: hire a party band

  22. Pingback: phoenix marketing companies

  23. Pingback: phoenix marketing solutions

  24. Pingback: phoenix seo agency

  25. Pingback: seo company in phoenix

  26. Pingback: bands to hire near me

  27. Pingback: band for corporate events

  28. Pingback: phoenix marketing solutions

  29. Pingback: wedding bands music

  30. Pingback: phoenix seo

  31. Pingback: wedding music band

  32. Pingback: seo agency phoenix

  33. Pingback: corporate event bands

  34. Hire a top phoenix marketing firms that is now available in addition at reasonable prices now only!

  35. Pingback: phoenix seo

  36. Pingback: bands for hire london

  37. Pingback: seo company in phoenix

  38. Pingback: phoenix marketing companies

  39. Pingback: bands for hire london

  40. Pingback: phoenix seo expert

  41. Pingback: phoenix seo services

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>