PDA

View Full Version : Caret position within a text field


pthompson2002
08-27-2002, 03:29 PM
I have posted previously on this subjec but have not found any joy with the solutions I was offered.

When a user clicks the cursor within the text field. I would like the position to be displayed under the textarea. Something along the lines of "Current Cursor Position : 7". How can i do this?

thanks

Pete

beetle
08-27-2002, 03:50 PM
Hey, I found your previous post. This link doesn't work for you?

http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130

Works just peachy for me :D

pthompson2002
08-28-2002, 08:51 AM
I have looked at that page but I can't seem to change it correctly so it does what I want it to do. Below is the code that I have created but when i click the cursor in the textarea it returns an 'undefined' where I want the position to be displayed, i.e

"Character Position : undefiend" when really I want
"Character Position : 7" for example

--------------------------------------------------------------------------------

<script language="javascript">
<!-- //Begin Hide
function checkMessage(myForm) {
if (/[\"\'\/\\]/.test(myForm.charcount.value)){
alert("Invalid Message! This message included invalid characters.")
return (false)
}
return (true)
}

function countit(what){
formcontent=what.form.charcount.value
displaycount.innerHTML=formcontent.length
}

function storeCaret(textE1) {
if (textE1.createTextRange)
textE1.caretPos = document.selection.createRange().duplicate();
displaycount.innerHTML=
}
// End Hide-->
</script>
</HEAD>
<BODY>
<form onSubmit="return checkMessage(this)">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" colspan="2">
<textarea rows="12" name="charcount" onkeyup="return countit(this)" cols="60" wrap="virtual" onclick="storeCaret(this);"></textarea>
</td>
</tr>
<tr>
<td valign="top" width="35%">Current Character Position:</td>
<td><div id="displaycount" size="20">&nbsp;&nbsp;</div></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td align=left><input type="submit" value="Submit" id=submit1 name=submit1>
&nbsp;<input type="reset" value="Reset" id=reset1 name=reset1></td>
</tr>
</table>
</form>
-------------------------------------------------------------

pthompson2002
08-28-2002, 08:52 AM
sorry, the

displaycount.innerHTML=

should be

displaycount.innerHTML= texte1.caretPos