WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Pasting text at selection in Textarea, Idris, July 3, 2004
     Re:Pasting text at selection in Textarea, Erik Arvidsson, July 19, 2004

Subject: Re:Pasting text at selection in Textarea From: Erik Arvidsson Date: July 19, 2004
This is pretty simple to do in IE. Use a range and modify the range.

function setSelectionStart( el, n )
{
var r = el.createTextRange();
r.collapse();
r.move( "character", nSelectionStart );
r.select();
}

function getSelectionStart( el )
{
var r = el.createTextRange();
var sr = el.ownerDocument.selection.createRange();
if ( !el.contains( sr.parentElement() ) )
return -1;
r.setEndPoint( "EndToStart", sr );
return r.text.length;
}

function setSelectionText( el, s )
{
var sr = el.ownerDocument.selection.createRange();
if ( !el.contains( sr.parentElement() ) )
return;
sr.text = sText;
}

function getSelectionText( el )
{
var sr = el.ownerDocument.selection.createRange();
if ( !el.contains( sr.parentElement() ) )
return "";
return sr.text;
}

I'll leave selectionEnd and selectionStart as an exercise. You'll probably have to look at the MSDN reference for the TextRange object interface.

erik

I am trying to make buttons for a textarea so that, for example, you can click "Bold" and it will put [B] and [/B] tags around what you are selected on. In Mozilla, selectionStart and selectionEnd works very well, but in IE, I can't find a clean way to do it. I looked at some of the bulletin boards like phpBB and they seem to have strange workarounds. Here is a page that has that kind of workaround: http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
Is there a simpler way?
Thanks in advance,
Idris



Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX