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.

2
  • Clarify your question.What do you want to highlight, is it the div containing text? Commented Jun 5, 2012 at 13:58
  • The content inside entity id should be highlighted inside article id.. Commented Jun 5, 2012 at 14:02

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

5 Comments

Could you tell me what this statement does ' var entityRegularExpression = new RegExp(entityText,"g")' ?
It creates a regular expression using the content of 'entityText', the 'g' stands for global. So, if 'entityText = "foo"' the regular expression will look for all the occurrences of 'foo' in a string. i.e. its equivalent to '/foo/g'.
do u know what to do if a . or $ symbol comes in between the matching wrds... using this form, the $ or dot symbol wont get highlighted..
0

Just replace yourDesireString with something like <span class='highlight'>yourDesireString</span>.

span.highlight {
     background-color: yellow;
}

2 Comments

it will nt be fixed one...Ive just given a static example..what if the content im going to highlight is generated realtime
So do the replace job dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.