How to search text and highlight the text that matches in a webpage.

Description :

Use the following function and pass the string to be searched in the function as parameter.

var n = 0;
function Findstring(searchstring) {

if (document.all) {
txt = window.document.body.createTextRange();//create a text range i.e all text in webpage
for (i = 0; i <= n && (found = txt.findText(searchstring)) != false; i++) {
txt.moveStart("character", 1);//move to next
txt.moveEnd("textedit");//move to end
if (found) {
txt.moveStart("character", -1);
txt.findText(searchstring);;//hightlight the text
else {
if (n > 0) {
n = 0;
else {
alert("Sorry, we couldn't find.Try again");
return false;

It will highlight the string if any matched case will be found .It works in IE4+.

Article by Abhay Kumar Raut
DNS MVM and .Net Guru Team Lead at CirrusCloud Systems Bhubaneswar,Odisha Thanks and Regards, Abhay Kumar Raut

Follow Abhay Kumar Raut or read 39 articles authored by Abhay Kumar Raut


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: