Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Determine charat position in text area

Reply
Thread Tools

Determine charat position in text area

 
 
jen_designs@hotmail.com
Guest
Posts: n/a
 
      06-21-2005
I found the following script that inserts a string into a textarea.
What I need to find is the cursor position returned as a number. For
instance if I have a string ABCD and the cursor is between A and B, the
postion would be 1. Any suggestions?

<html>
<head>
<SCRIPT>
function storeCaret(textEl)
{
textEl.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret(textEl, text)
{
if (textEl.createTextRange && textEl.caretPos)
{
var caretPos = textEl.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1)
== ' ' ? text + ' ' : text;
}
else
{
textEl.value = text;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
<TEXTAREA NAME="Content" ROWS="10" COLS="80" WRAP="soft"
ONSELECT="storeCaret(this);" ONCLICK="storeCaret(this);"
ONKEYUP="storeCaret(this);"></TEXTAREA>
<BR>
<INPUT TYPE="text" NAME="bold" SIZE="80" VALUE="">
<INPUT TYPE="button" VALUE="insert"
ONCLICK="insertAtCaret(this.form.Content, this.form.bold.value);">
</FORM>
</body>
</html>

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      07-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I found the following script that inserts a string into a textarea.
> What I need to find is the cursor position returned as a number. For
> instance if I have a string ABCD and the cursor is between A and B,
> the postion would be 1. Any suggestions?


STFW.

> [Invalid HTML snipped -- see <http://validator.w3.org/> for details]



PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String.charAt() returns wrong char column.column@gmail.com Java 11 03-25-2008 02:05 AM
Charat Control (date Vs date) LearnMore.John@gmail.com ASP .Net Web Controls 2 11-28-2006 09:15 AM
StringBuffer charAt ?? how .. any other way under J2ME??? THANK YOU! jason@cyberpine.com Java 1 12-27-2005 04:07 PM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
Revealing the position of the cursor in a text-area PeP Javascript 3 10-23-2004 08:35 AM



Advertisments