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

79 thoughts on “Highlight text using jQuery and the HTML5 mark tag

  1. Pingback: Tweets that mention Highlight text using #jQuery and the #HTML5 #mark tag Highl... -- Topsy.com

  2. I am using mark dynamically, I intend to use this as a highlighter tool in my webpage.. figuring out a way to select text and clicking on the button to mark text on my web page, then committing the same.

    Am i going in a right direction?

  3. Hi,

    How can a user select a text an a HTML page and the selected text gets highlighted automatically ?

  4. Hi Alan,

    Thanks for the link. Could I request for some help in highlighting the text? Can we use tag of HTML 5?

  5. Good stuff. Is it possible though to search and highlight partial word matches? Currently it only finds full word matches.

  6. Please let me know if you’re looking for a author for your blog.
    You have some really great articles and I feel I would be a good asset.

    If you ever want to take some of the load off, I’d absolutely love
    to write some material for your blog in exchange for a link
    back to mine. Please send me an email if interested.
    Thank you!

  7. Hi to every single one, it’s genuinely a nice for me to visit this site,
    it consists of valuable Information.

  8. Hi there I am so grateful I found your weblog, I really found you by error, while I was looking on Digg for something else, Regardless I am here now and would
    just like to say cheers for a marvelous post and a all round interesting blog (I also love
    the theme/design), I don’t have time to look over it all at the minute but I
    have saved it and also added your RSS feeds, so when I have time I will be back to
    read much more, Please do keep up the great job.

  9. , Philip dear, I am certain there extiss a thing incorrect; that horrible Hunt has been here using a extremely quiet, soft-spoken old gentleman, and they have been going on with my poor Pa about my wrongs and his his, indeed! and they have worked him up to feel that somebody has cheated his daughter out of fantastic fortune; and who can that somebody be but your father? And whenever they see me coming, papa and that horrid Hunt go off for the Admiral Byng:’ and a single night when Pa came residence he said, Bless you, bless you, my poor, innocent, injured kid; and blessed you’ll be, mark a fond father’s words!’ They may be scheming a thing against Philip and Philip’s father. Mr. Bond the soft-spoken old gentleman’s name is: and twice there has been a Mr. Walls to inquire if Mr. Hunt was at our residence.’ A gentleman of your name of Bond was uncle Twysden’s attorney. An old gentleman, using a bald head, and a single eye larger than the other?e2809dWell, this old man has a single smaller sized than the other, I do believe,’ says Caroline. First man who came was Mr. Walls a rattling young fashionable chap, constantly laughing, speaking about theatres, operas, almost everything came residence from the Byng’ coupled with Pa and his new buddy oh! I do hate him, that man, that Hunt! then he brought the old man, this Mr. Bond. What are they scheming against you, Philip? I tell you this matter is all about you and your father.’19b1bb

  10. Great article! This is the type of info that are supposed to be
    shared across the web. Disgrace on Google for now not positioning this submit higher!
    Come on over and seek advice from my site . Thank you
    =)

  11. This is very fascinating, You’re an excessively professional blogger.
    I have joined your feed and look forward to seeking extra of your
    wonderful post. Also, I have shared your website in my social networks

  12. My Partner And I merely need to notify you the fact that I am novice to writing and totally cherished your work. Very likely I am most likely to save your site post . You really have wonderful article material. Like it for giving out with us your domain article.

  13. Great awesome issues here. I am very satisfied to see your article. Thanks a lot and i am looking ahead to contact you. Will you kindly drop me a mail?

  14. Thanks for the writeup. I definitely agree with what you might be saying. I’ve been talking about this topic a whole lot lately with my father so hopefully this will get him to see my point of view. Fingers crossed!

  15. excellent points altogether, you simply won a brand new reader. What could you recommend about your submit that you just made some days in the past? Any sure?

  16. Thank you, I’ve just been searching for information approximately this topic for a while and yours is the greatest I have found out so far. However, what about the conclusion? Are you positive about the supply?

  17. It was any exhilaration discovering your site yesterday. I arrived here nowadays hunting new items. I was not necessarily frustrated. Your suggestions soon after new approaches on this thing have been helpful plus an superb assistance to personally. We appreciate you leaving out time to write out these items and then for revealing your thoughts.

  18. I¡¦ve recently started a web site, the info you provide on this website has helped me greatly. Thank you for all of your time & work.

  19. Thank you for any other excellent post. The place else may just anyone get that kind of information in such an ideal manner of writing? I’ve a presentation next week, and I am on the search for such info.

  20. Hmm is anyone else experiencing troubles with the images on this blog loading? I’m trying to discover out if its a difficulty on my finish or if it’s the weblog. Any feed-back would be greatly appreciated.

  21. Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just too fantastic. I really like what you’ve acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still care for to keep it smart. I can not wait to read far more from you. This is actually a tremendous site.

  22. Very interesting information!Perfect just what I was looking for! “I have a hundred times wished that one could resign life as an officer resigns a commission.” by Robert Burns.

  23. But a smiling visitor here to share the love (:, btw outstanding layout.

  24. Simply a smiling visitor here to share the love (:, btw outstanding design and style .

  25. You have observed very interesting details ! ps nice website . “He that will not sail till all dangers are over must never put to sea.” by Thomas Fuller.

  26. Hello my loved one! I wish to say that this post is amazing, great written and come with approximately all significant infos. I would like to see extra posts like this.

  27. Hi my loved one! I wish to say that this article is amazing, great written and come with approximately all important infos. I would like to peer extra posts like this.

  28. I do like the way you have framed this specific issue and it does offer me some fodder for thought. Nonetheless, because of what precisely I have witnessed, I simply just wish when other responses pile on that individuals keep on point and in no way embark on a tirade regarding some other news du jour. Still, thank you for this exceptional piece and while I can not necessarily concur with this in totality, I value the standpoint.

  29. My sister saved this internet site for me and I have been going through it for the past several hrs. This is really going to benefit me and my classmates for our class project. By the way, I like the way you write.

  30. I do agree with all the ideas you have presented on your post. They are really convincing and can certainly work. Still, the posts are very brief for starters. May just you please lengthen them a little from next time? Thank you for the post.

  31. hi!,I really like your writing so so much! percentage we communicate more approximately your post on AOL? I need a specialist on this space to unravel my problem. Maybe that is you! Looking forward to see you.

  32. I’ve been browsing on-line greater than 3 hours today, yet I by no means found any interesting article like yours. It is pretty worth sufficient for me. In my opinion, if all web owners and bloggers made good content as you probably did, the internet will be much more useful than ever before. “I thank God for my handicaps, for through them, I have found myself, my work and my God.” by Hellen Keller.

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>