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 8052195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:29:11+00:00 2026-06-05T07:29:11+00:00

I am trying to use a text highlight option using javascript.I know how to

  • 0

I am trying to use a text highlight option using javascript.I know how to find the start postiion of the string, bt how do I make it highlight. my code looks like

<div id="entity">health insurance</div>
<div id="article">This kind of insurance is meant to supplement health insurance for cancer-care costs. But generally you're better off putting your money toward comprehensive health policies. </div>

and the javascript that I have used it

$(document).ready(function()
{
var test=document.getElementById('article').innerHTML;
var variable=document.getElementById('entity').innerHTML;
alert(test.search(new RegExp(variable, "i")));
});

This will alert the start postion of the string.

  • 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-06-05T07:29:12+00:00Added an answer on June 5, 2026 at 7:29 am

    HTML:

    <div id="entity">health insurance</div>
    <div id="article">This kind of insurance is meant to supplement health insurance for cancer-care costs. But generally you're better off putting your money toward comprehensive health policies. </div>​
    

    CSS:

    .highlight {
      background-color: yellow
    }
    

    Javascript:

    $(document).ready(function () {​
      var $test = $('#article');
      var entityText = $('#entity').html();
      var highlight = '<span class="highlight">' + entityText + '</span>';
      $test.html($test.html().replace(entityText, highlight));
    }
    

    Demo: http://jsfiddle.net/ehzPQ/2/

    So, I’m just replacing the first occurrence of the ‘entity’ string with the same string wrapped up in a span class.

    Did I understood your problem right?

    ———————–UPDATE————————-

    If you want to highlight all the occurrences modify use a regular expression:

    Updated Javascript:

    $(document).ready(function () {
      var $test = $('#article');
      var entityText = $('#entity').html();
      var entityRegularExpression = new RegExp("\\b" + entityText + "\\b", "gi");
      var highlight = '<span class="highlight">' + entityText + '</span>';
      $test.html($test.html().replace(entityRegularExpression, highlight))
    }
    

    And updated demo: http://jsfiddle.net/ehzPQ/3/

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

Sidebar

Related Questions

I am trying to use jQuery to find <td> s with given content (text)
I'm trying to highlight every digit sequence in a text using the Highlight method.
I'm trying to use XSLT text output to generate a file (in a file
I'm trying to use the text value from a text field as a float
I'm trying to use quoted text in one of node's attributes: var network_json =
I am trying to use GDI+ to draw text onto an image, however, I
I have been trying to use SharePoint:InputFormTextBox as Rich Text Editor in my SharePoint
I'm trying to use Worknik's API to iterate each text definition of a given
I am trying to use XML::RAI perl module on UTF8 coded text and I
I'm trying to use the RegisteredWindowMessage API function to send text from one application

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.