Link to home
Start Free TrialLog in
Avatar of rfila
rfila

asked on

Move Cursor to the end of selected textarea

Hi

I am trying to create a textarea which when clicked on will add the current date, time & username.  I have got that working fine but after adding the data to the textarea the cursor always moves back to the beginning.  Can anyone point me in the direction of the function or functions I can use to move the cursor to the end of the textarea ?

Thanks
Richard

Current Code to have cursor move commands added to it :
<script language="javascript">

function addupdate()
      {
      document.testform.testbox.value=document.testform.testbox.value + "\nThu 7/8/2003 RF: ";
      }

</script>

<form method="POST" name="testform">
<textarea rows="14"
  name="testbox" cols="41" onFocus="addupdate();">LALALALLA
LAALALAL
LALADsefgfsdgdf
</textarea>
</form>
Avatar of gabss
gabss

have you tried focus ?

Avatar of rfila

ASKER

Well I have now (code enclosed) and still the cursor moves to the beginning:

<script language="javascript">
function addupdate(refuser)
      {
      refuser.value=refuser.value + "\nWed 9/7/2003 RF: ";
      refuser.focus();
      }
</script>
Avatar of David H.H.Lee
try this :

<TEXTAREA name="message" cols=50 rows=7 wrap="soft" scrolling="no"
        onselect="store(this);" onclick="store(this);" onkeyup="store(this);" onchange="store(this);"></TEXTAREA>

<script language="javascript">
<!--
function store(ftext) {
      if (ftext.createTextRange) {
            ftext.caretPos = document.selection.createRange().duplicate();
      }
}

function AddText(Msg)
{
      if (document.frmPostMsg.message.createTextRange && document.frmPostMsg.message.caretPos) {
            var caretPos = document.testform.message.caretPos;
            caretPos.text = Msg;
      } else {
            document.testform.message.value+=Msg;
      }
      document.testform.message.focus();
}

function getText() {
      if (document.frmPostMsg.message.createTextRange && document.testform.message.caretPos) {
            return document.testform.message.caretPos.text;
      } else {
            return '';
      }
}

function addupdate()
    {
       AddText("\nThu 7/8/2003 RF: ");
    }

//-->
</script>

It work for me. Hope that solve your problems.

Regards

x_com    
som mistake at
function AddText(Msg)
{
    if (document.frmPostMsg.message.createTextRange && document.frmPostMsg.message.caretPos) {
 
change to :

function AddText(Msg)
{
    if (document.testform.message.createTextRange && document.testform.message.caretPos) {
 
Avatar of rfila

ASKER

Hi

That dosn't really help much  -  the script is all wrong and far too complicated.  Surely it is a small job moving the cursor to the end of the line ?
hi rfila

Try this :
Full source code :

Test.html
==============
<html>
<head>
<script language="javascript">
<!--
function store(ftext) {
    if (ftext.createTextRange) {
         ftext.caretPos = document.selection.createRange().duplicate();
    }
}

function AddText(Msg)
{
    if (document.testform.message.createTextRange && document.testform.message.caretPos) {
         var caretPos = document.testform.message.caretPos;
         caretPos.text = Msg;
    } else {
         document.testform.message.value+=Msg;
    }
    document.testform.message.focus();
}

function getText() {
    if (document.testform.message.createTextRange && document.testform.message.caretPos) {
         return document.testform.message.caretPos.text;
    } else {
         return '';
    }
}

function addupdate()
   {
      AddText("\nThu 7/8/2003 RF: ");
   }

//-->
</script>
</head>
<body>
<form name="testform">
<TEXTAREA name="message" cols=50 rows=7 wrap="soft" scrolling="no"
       onselect="store(this);" onclick="store(this);" onkeyup="store(this);" onchange="store(this);">
TESTING TESTING TESTING
</TEXTAREA><br><br>
<input type="button" onClick="javascript:addupdate()">
</form>
</body>
</html>

Hope that solve.

Regards
x_com
Avatar of rfila

ASKER

kind of works - cursor moves to beginning of addition - what about the gettext function - it isnt being used ???
hi rfila,
you may eliminate the getText function. I used it for debug purpose in order to solve your problems.

So, solve the problems?

x_com
This kind of test --

if (document.testform.message.createTextRange && document.testform.message.caretPos) {

is IE specific and does not work in other browsers.  If that's what you want, fine, however, why not do it much simpler --

just get the string name of the text area and do string.append(" ");

appending a space to the end sets the cursor to the end of the string, it's that simple!
Avatar of rfila

ASKER

that sounds like what i am looking for - it only needs to work in IE - however, neither of the following examples work !

<script language="javascript">

function stamptoday(usethis)
      {
      usethis.append("\nThu 07/08/2003 RF:");
      }
</script>
<textarea rows="14"   name="testbox" cols="41" onFocus="stamptoday(this.value);" >LALALALLA</textarea>
<textarea rows="14"   name="testbox" cols="41" onFocus="stamptoday(this);" >LALALALLA</textarea>
OK, if you know what you want to add, it's a lot simpler --

newString = usethis + "\nThu 07/08/2003 RF:";

will just add the value in quotes to the end of the string --

then to get back --

usethis = newString;

or all in one line ---

usethis = usethis + "\nThu 07/08/2003 RF:";  --which adds and reassigns at the same time.

or

usethis += "\nThu 07/08/2003 RF:";  -- that looks wierd, but it supposedly adds and appends at the same time.
BTW -- what I gave you works in all browsers, nothing IE specific.
Avatar of rfila

ASKER

Yes that's all well and good but what about the cursor ?  The whole point of this thread is to get the cursor to be at the end of the textbox after the new data has been added to it.

<html>
<head>
<script language="javascript">

function stamptoday(usethis)
      {
      usethis.value += "\nThu 07/08/2003 RF:";
      }

</script>
</head>
<body>

<form method="POST" name="testingthisform" action="--WEBBOT-SELF--">
<p><textarea rows="14" name="testarea1" cols="41" onfocus="stamptoday(this);" >Text here in this box</textarea>
</p>

</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of sciwriter
sciwriter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any updates, rfila?

Regards
x_com
Avatar of rfila

ASKER

This method works perfectly and is a sensible length too.

I reworked the script as below to insert date and initials upon focus of a textarea.  Obviously I have just keyed the date but I will use PHP to put that in later.  Thanks sciwriter !

<html>
<head>
<SCRIPT>
function setCaretToEnd (el) {
 if (el.createTextRange) {
   var v = el.value;
   var r = el.createTextRange();
   r.moveStart('character', v.length);
   r.select();
 }
}
function insertAtEnd (el, txt) {
 el.value += txt;
 setCaretToEnd (el);
}
</SCRIPT>

</head>
<body>

<form method="POST" name="testingthisform" action="--WEBBOT-SELF--">
<p><textarea rows="14" name="testarea1" cols="41" onFocus="insertAtEnd (this.form.testarea1, 'Thu 31/07/2003, RF:');"
 >Text here in this box</textarea>
</p>

</form>
</body>
</html>