...

View Full Version : How can i set the (caret??) mouse pointer in the end of string



frontline
10-01-2003, 01:31 PM
Hello
i inserting simple string into textarea (whit js txt.value="blah" ) now i like to to see the
caret (im not sure if its name is caret , but i mean the blinking vertical line that blinks when we type in textarea)
in the end of the string i entered , what i tried to do is to put right after the txt.value="blah"
txt.focus();
it do give me that "blinking line" but only at the begging of the string.
so my question is how to move it to the end of the string.
thanks

requestcode
10-01-2003, 02:14 PM
Take a look at this post:
http://www.codingforums.com/showthread.php?s=&threadid=8759&highlight=caret

beetle
10-01-2003, 08:11 PM
var ta = document.forms[0].elements['someTextArea'];
var r = ta.createTextRange();
r.expand( "textedit" );
r.collapse( false );
r.select();

Mr J
10-02-2003, 12:00 AM
This worked a while ago


txt.value="blah"
txt.focus();
txt.value+=""

frontline
10-02-2003, 04:17 PM
Thanks for all to the fast replies

now im facing new problem say i have the string "foo1 foo2" in the textarea
now i like to insert new string between "foo1" and "foo2" say it will be "foo3" so it will look like "foo1 foo3 foo2"
how can i keep the "blinking vertical line" on the end of this new string ("foo3") ?
thanks

Vincent Puglia
10-03-2003, 05:37 AM
Hi,
Is this what you're looking for?



<html>
<head>
<script language='javascript' type='text/javascript'>
function storeCaret (txtObj)
{
if (txtObj.createTextRange)
txtObj.cPos = document.selection.createRange().duplicate();
}

function insertAtCaret (txtObj, text)
{
if (txtObj.createTextRange && txtObj.cPos)
{
var cPos = txtObj.cPos;
cPos.text =
cPos.text.charAt(cPos.text.length - 1) == ' ' ?
text + ' ' : text;
}
else
txtObj.value = text;

txtObj.cPos.select();
}
</script>
</head>
<body>
<form name="a">
<textarea name="b" wrap="soft" rows="5" cols='30' onselect="storeCaret(this);" onclick = "storeCaret(this);" onkeyup="storeCaret(this);">
foo1 foo2
</textarea>
<br>
<input type="text" name="c" value="foo3">
<br>
<input type="button" value="insert at caret"
onclick="insertAtCaret(this.form.b, this.form.c.value);">
</form>
</body>
</html>


Vinny



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum