Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #19826

Re: How to make selected text (a range of a text block) with red background color?

Newsgroups comp.lang.javascript
Date 2013-05-19 14:47 -0700
References <eadac30e-3421-4f40-aa3b-e0dda2219b7a@googlegroups.com> <0898b333-e573-4154-bc88-086bd23326e2@z8g2000yqd.googlegroups.com>
Message-ID <f9eb52ba-e0af-4fda-87e2-b3344282d37f@googlegroups.com> (permalink)
Subject Re: How to make selected text (a range of a text block) with red background color?
From justaguy <lichunshen84@gmail.com>

Show all headers | View raw


>On Sunday, May 19, 2013 5:04:14 PM UTC-4, Michael Haufe (TNO) wrote:
> Here is a basic approach to get you started (some assembly required):
> 
> 
> 
> function highlight(node,text){
> 
>     var html = node.innerHTML,
> 
>         i = html.indexOf(text),
> 
>         len = text.length;
> 
>         if(i > -1){
> 
>             node.innerHTML = html.substring(0,i) +
> 
>                 "<span class='hightlight'>" +
> 
>                      html.substring(i,i + len) +
> 
>                 "</span>" +
> 
>                 html.substring(i + len);
> 
>         }
> 
> }
> 
> 
> 
> var node = document.getElementById("foo")
> 
> highlight(node,"rail")
> 

If my understanding is correct, the problem with this solution is that,
we don't know that "rail" would be selected beforehand.

 
> 
> 
> 
> On May 19, 2:20 pm, justaguy <lichunshe...@gmail.com> wrote:
> 
> > Hi,
> 
> >
> 
> > Say, I have the following block of text:
> 
> > "
> 
> > Commuters are bracing for a difficult trip around southwest Connecticut and to New York City beginning Monday as workers repair the Metro-North commuterrailline crippled by a derailment and crash.  Crews will spend days rebuilding 2,000 feet of track, overhead wires and signals following the collision...
> 
> > "
> 
> > And I want to have an ability to let a user to select, say, "rail" and "crash" with red background color, how?
> 
> > With Firefox and Chrome.
> 
> >
> 
> > Many thanks.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-19 12:20 -0700
  Re: How to make selected text (a range of a text block) with red background color? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-05-19 14:04 -0700
    Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-19 14:47 -0700
      Re: How to make selected text (a range of a text block) with red background color? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-05-19 15:24 -0700
  Re: How to make selected text (a range of a text block) with red background color? Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-05-19 14:22 -0700
    Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-19 15:58 -0700
      Re: How to make selected text (a range of a text block) with red background color? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-05-19 16:05 -0700
        Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-19 17:59 -0700
          Re: How to make selected text (a range of a text block) with red background color? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-05-19 18:45 -0700
  Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-19 14:50 -0700
    Re: How to make selected text (a range of a text block) with red background color? JJ <duh@nah.meh> - 2013-05-21 06:06 +0700
      Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-21 10:05 -0700
        Re: How to make selected text (a range of a text block) with red background color? JJ <duh@nah.meh> - 2013-05-22 00:54 +0700
          Re: How to make selected text (a range of a text block) with red background color? justaguy <lichunshen84@gmail.com> - 2013-05-21 21:25 -0700
        Re: How to make selected text (a range of a text block) with red background color? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-21 22:38 +0200

csiph-web