K
KenA
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?
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?