Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6337067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:10:53+00:00 2026-05-24T19:10:53+00:00

Possible Duplicate: Highlight a word with jQuery I want to highlight certain words in

  • 0

Possible Duplicate:
Highlight a word with jQuery

I want to highlight certain words in html <body> tag, but I want to avoid highlighting those which are within <a> tag. How can this be done? By highlight I mean, underlining and making word bold.

For e.g I have this html content

<body>
<p>
This is sample text for the purpose of highlighting searched text. Please visit <a href="javascript:void(0)">www.sampleSite.com</a>
</p>
</body> 

Now when I search for the word “sample”, I would not want to highlight the word sample contained within that anchor tag.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:10:54+00:00Added an answer on May 24, 2026 at 7:10 pm

    1. jQuery plugin to do highlighting:

    /**
    * jQuery plugin to replace text strings
    *
    * Taken from @link http://net.tutsplus.com/tutorials/javascript-ajax/spotlight-jquery-replacetext/
    */
    
    $.fn.replaceText = function( search, replace, text_only ) {
    return this.each(function(){
            var node = this.firstChild,
            val, new_val, remove = [];
            if ( node ) {
                do {
                  if ( node.nodeType === 3 ) {
                    val = node.nodeValue;
                    new_val = val.replace( search, replace );
                    if ( new_val !== val ) {
                      if ( !text_only && /</.test( new_val ) ) {
                        $(node).before( new_val );
                        remove.push( node );
                      } else {
                        node.nodeValue = new_val;
                      }
                    }
                  }
                } while ( node = node.nextSibling );
            }
            remove.length && $(remove).remove();
        });
    };
    

    2. CSS to style a span element with a single highlight class

    span.highlight{
        font-weight: bold;
        text-decoration: underline;
    }
    

    3. jQuery code to run after you have included the plugin & style:

    // replace partial string occurrences with a span css styled element
    // i.e. textsample => text<span class="highlight">sample</span>
    $('p').replaceText(/(sample)/gi, '<span class="highlight">$1</span>');
    

    OR as suggested by @Nick C. in the comments

    // replace full string occurrences with a span css styled element
    // i.e. ONLY sample => <span class="highlight">sample</span>
    $('p').replaceText(/\b(sample)\b/gi, '<span class="highlight">$1</span>');
    

    Here is a demo

    The plugin is smart enough not to replace the element’s attributes, or text inside elements such as a element…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ templates that accept only certain types For example, if we want
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Highlight keywords in a paragraph I have been struggling with the idea
Possible Duplicate: javascript WYSIWYG HTML editors? Im not quite sure how to put this
Possible Duplicate: highlighting search results in php/mysql I am doing a search with a
Possible Duplicate: How to highlight the results of a text in a gridview? I've
Possible Duplicate: How to use Twitter Bootstrap popovers for jQuery validation notifications? Trying to
Possible Duplicate: JQuery Datepicker - no default date When no default date is given
Possible Duplicate: RegEx match open tags except XHTML self-contained tags I have a HTML
Possible Duplicate: How can I disable the UITableView selection highlighting? I am programatically making

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.