Re: Replacing TextArea´s Selected Text...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: clintonG (csgallagher_at_REMOVETHISTEXTmetromilwaukee.com)
Date: 12/14/04


Date: Mon, 13 Dec 2004 18:40:46 -0600

Client-side JavaScript is required. You can get JavaScript(s) that will get
as close as possible and enable your form to function exactly as you are
seeking from the JavaScript FAQ [1] but you're probably going to have to
hire somebody to wire everything up and add the or change your requirements
as this objective is not too easy to do using JavaScript and would be much
easier with a trip back to the server which would return the text with new
style applied.

-- 
<%= Clinton Gallagher, "Twice the Results  -- Half the Cost"
         Architectural & e-Business Consulting -- Software Development
         NET csgallagher@REMOVETHISTEXTmetromilwaukee.com
         URL http://www.metromilwaukee.com/clintongallagher/
[1] http://developer.irt.org/
"KenA" <ken_awamura@REMOVEhotmail.com> wrote in message
news:uWhVmVU4EHA.1400@TK2MSFTNGP11.phx.gbl...
> Hi. Suppose I have a textarea with some text in it and after i make a text
> selection I want to replcae the selected text with for example the same
text
> but with anothet color. I have the folowing html page:
>
> <form name="Form1">
>  <textarea name="txtArea1" cols="40" rows="4"></textarea>
> </form>
> <script type="text/javascript">
>  document.Form1.txtArea1.value = "Now go ahead Now go ahead";
> </script>
>
> In case the browser is Gecko I can do the following:
>
> <script type="text/javascript">
>
>  var color = "#FF0000";
>
>  function DoSelectionGecko()
>  {
>   var s = this.Form1.txtArea1.value;
>   //alert( "Before selection: " + s.substring( 0,
> this.Form1.txtArea1.selectionStart) );
>   //alert( "From selection: " +
> s.substring(this.Form1.txtArea1.selectionStart) );
>   //alert( "After selection: " +
> s.substring(this.Form1.txtArea1.selectionEnd) );
>   //alert( "Selected: " + s.substring( this.Form1.txtArea1.selectionStart,
> this.Form1.txtArea1.selectionEnd ) );
>
>   rng = s.substring( this.Form1.txtArea1.selectionStart,
> this.Form1.txtArea1.selectionEnd )
>
>   var newRng = "<span style=color:'" + color + "';>" + rng + "</span>"
>
>   var txtBefore = s.substring( 0, this.Form1.txtArea1.selectionStart);
>   var txtFrom = s.substring(this.Form1.txtArea1.selectionStart);
>   var txtSelected = s.substring( this.Form1.txtArea1.selectionStart,
> this.Form1.txtArea1.selectionEnd );
>
>   if( txtSelected.length>0 )
>    this.Form1.txtArea1.value = txtBefore + txtFrom.replace( rng, newRng );
>
>  };
>
>  function DoSelectionIE()
>  {
>   var s = this.Form1.txtArea1.value;
>
>   var selection = document.selection;
>
>   alert( event.clientX );
>   alert( event.clientY );
>
>   if( selection != null )
>   {
>    rng = selection.createRange();
>    rng.moveToPoint(event.clientX, event.clientY);
>    rng.pasteHTML( "<span style=color:'" + color + "';>" + rng.htmlText +
> "</span>" );
>   }
>
>   //alert( rng.text );
>  };
>
>
>  if( document.getSelection )
>  {
>   if( window.Event )
>   {
>    document.captureEvents( Event.MouseUp );
>   }
>   document.onmouseup = DoSelectionGecko;
>  }
>  else if( document.selection && document.selection.createRange )
>  {
>   if( window.Event )
>   {
>    document.captureEvents( Event.MouseUp );
>   }
>   document.onmouseup = DoSelectionIE;
>  }
>  else
>  {
>   alert( "Not Supported in your Browser!" );
>  }
>
> </script>
>
> Well, the problem is with IE, since the selected text is NOT in the body
of
> the doc, but instead it´s inside the textArea, the rng.pasteHTML is not
> working, probably because it´s targeting the body instead of the textarea.
>
> Any tips on how to accomplish that?
>
>


Relevant Pages

  • Re: Replacing TextArea´s Selected Text...
    ... Suppose I have a textarea with some text in it and after i make a text> selection I want to replcae the selected text with for example the same text> but with anothet color. ... > function DoSelectionGecko() ... > var s = this.Form1.txtArea1.value; ...
    (microsoft.public.frontpage.programming)
  • Replacing TextArea´s Selected Text...
    ... Suppose I have a textarea with some text in it and after i make a text ... selection I want to replcae the selected text with for example the same text ... function DoSelectionGecko() ... var txtFrom = s.substring; ...
    (microsoft.public.frontpage.programming)
  • Re: Replace selected text in a textarea?
    ... How do I replace selected text in a textarea with JavaScript? ... selection with the new text. ... var ieSelection = document.selection.createRange; ...
    (comp.lang.javascript)
  • Re: "Professional Javascript" review
    ... a null value is an empty object pointer" ... In java ... might be better off abandoned for javascript programming. ... var a, b, c, d, ...
    (comp.lang.javascript)
  • Re: on page exit
    ... of which JavaScript is only one. ... used in HTML with intrinsic event handler attribute values, ... var clickedLink = null; ...
    (comp.lang.javascript)