How can I select multiple tetx on web page?

I knew how to use
txt=document.body.createTextRange();
txt.findText(str)
txt.select();

to select str on web page, but i only one str can be selected using this method, I want to select all the str on page, what can I do? is it possible?
LVL 5
mikezangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
Select not going to happen, if you just want to highlight for a search string use this.

This will work for all modern browsers.  Tested on IE6 and FF1



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <title> Search and Highlight </title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <script type="text/javascript">
   save=false;
   function setBlue(param,src)
   {
      str= new String(param);
      arg= new RegExp(param,'gi');
      src.value='';
      x=new String(document.body.innerHTML);
      save=x;
      x=x.replace(arg,str.fontcolor('blue'));
      document.body.innerHTML=x;
   }
   function clearBlue()
   {
      if(save) document.body.innerHTML=save;
   }
  </script>
</head>
<body>
<form>
Search For:<input type="text" name="sArg">
<input type="button" value="Highlight" onclick="setBlue(this.form.sArg.value,this.form.sArg)">
<input type="button" value="Clear" onclick="clearBlue()">
</form>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
<p>
Lorem ipsum dolor sit amet, lacus nonummy tempus, dui nascetur. Sociosqu luctus habitasse. Tempus dictumst curabitur sit id malesuada ipsum, at facilisi dignissim. Hendrerit potenti. Convallis nam. Cursus inceptos risus pretium nec aliquam nascetur. Ultricies vivamus eros congue conubia bibendum magnis nullam malesuada nam ultrices placerat iaculis.
Blandit eu nunc egestas sociosqu cubilia sagittis a mollis nascetur. Nisl euismod curae vivamus. Eleifend fermentum erat. Tempus semper hymenaeos scelerisque massa, purus cursus mus sociis libero. Eu fermentum sodales. Integer ac vestibulum wisi facilisis consequat, vitae mattis enim donec. Molestie orci.</p>
<p>
A dictumst sollicitudin quam habitasse integer netus scelerisque neque orci. Luctus hendrerit quam tempus iaculis praesent tempor per, sociosqu sollicitudin dictum bibendum. Nisl neque ornare viverra ultricies parturient cursus rhoncus inceptos mauris cursus vivamus faucibus. In orci class quis. Conubia condimentum sollicitudin scelerisque varius platea vivamus adipiscing, purus viverra. Nullam nisl elit est quis. Et, diam orci neque ultrices erat.
Habitant pretium. Suscipit ante. Volutpat suscipit odio. Nostra nunc ultrices eleifend egestas cursus. Diam cursus enim habitasse sed, sociis et. Sit interdum mi ridiculus pretium id, senectus tincidunt aenean sem pulvinar pellentesque. Et libero. </p>
</body>
</html>


Formatting options:
str.fontcolor('blue') can be replace by any of these string methods:
str.big()
str.blink()
str.bold()
str.fixed()
str.fontsize('5')
str.small()
str.strike()
str.sub()
str.sup()

Basically the properties of the font tag.  The whole thing is accompliched by Javascript inserting a font tag inline.

If you want the search to be case sensitive change the "gi" in the regular expression with "g".

If the search argument is a tag name, property attribute or a literal value found inside the tag, the page formatting may get screwed up, and some of the HTML may be displayed.  The only way around that would be with very complex filtering in the script, or processing individual text nodes instead of the body node itself.  That would be a substantial amount fo work and I'm not looking for a project.  You would probably have to hire someone if you want something that detail and robust.

Cd&
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikezangAuthor Commented:
Thank you very much.
It is ok even the selection isn't used. but I want to the highlight looks like the selection, I mean the text is displayed in turn over mode, is it posiible?
0
COBOLdinosaurCommented:
No the string methods on work on the text, the background is not exposed in the string object.

Cd&
0
mikezangAuthor Commented:
Thanks.
But I have a new question, the 'red'.fontcolor('red') is changed to '<FONT COLOR="red">red</FONT>', but I use that string in another string, something looks like as bwlow, it doesn't work!
str = "<A HREF=javascript:void onClick='document.write(\"green black <FONT COLOR="red">red</FONT>\");return false;'>COLOR</A>"

I need this one as below, what can I do?
str = "<A HREF=javascript:void onClick='document.write(\"green black <FONT COLOR=\\\"red\\\">red</FONT>\");return false;'>COLOR</A>"

0
COBOLdinosaurCommented:
I'd have to see more of the code to understand what you are doing, but if that sample is the way you are generating code, then this method is not going to work for you.  You need to re-think the design you are creating a maze of unmaintainable code.

Cd&
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.