Recommended Articles

8 Comments

  1. This code doesn’t apply the CSS anywhere, it just replaces all results with a lowercase version of the same words.
    It also has a syntax error.

    1. Hi X, thanks for your comment. Can you let us know which browser you are using and version? We will have a look and possibly update the code.

  2. This post does not work.You should first test this before posting. Or post a working demo

    1. Hi doorgo, Thanks for the comment. I will look into it. Can you please let me know which browser you are trying this and version of it?

  3. The only thing this doesn’t solve is removing the span that gets added, which didn’t matter in my case since resetting the search blows away the html and recreates it.

    function replaceText(searchword) {
    $(“body”).find(“.highlight”).removeClass(“highlight”);

    if (searchword != “”) {
    $(‘body’).each(function () {
    var r = $(this).html().replace(new RegExp(searchword, “ig”), function (match) {
    return “” + match + “”;
    });
    $(this).html(r);
    })
    }
    }

    1. hm apparently the span tags got removed in that post. Supposed to be:

      ” ” + match + “”;

      (obviously without the spaces)

      1. Okay one more try then I give up:

        “&lt span class=’highlight’ &gt” + match + “&lt / span &gt”;

        Hopefully everyone gets the point if this doesn’t work, you need to wrap the matched text in a span (or other element) that has the highlight class. That will add the highlight effect.

        1. Hi Jonathan, Thanks for your comments and appreciate the code.

Leave a Reply

Pin It on Pinterest

infoandapps logo
Highlight text in html page
Must have browser extensions for Digital Marketing
How to merge arrays or arrays of objects in javascript
Copyrighted.com Registered & Protected
Shares