C
charly
Greetings,
I got the following problem :
I have a input text which holds the following value : hello
Now, when the user click on the first l, a window.alert pops up and
prints 3.
Question : Is it possible to get the cursor's position in an input text?
Note I said input text, not a textarea.
So far, I've found this :
<html>
<head>
<script>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
alert(caretPos.text.length - 1);
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' '
? text + ' ' : text;
}
else
textEl.value = text;
}
</SCRIPT>
</head>
<body>
<form NAME="aForm">
<textarea NAME="aTextArea" ROWS="5" COLS="80" WRAP="soft"
ONSELECT="storeCaret(this);"
ONCLICK="storeCaret(this);"
ONKEYUP="storeCaret(this);"Kibology for all.
All for Kibology.
</textarea>
<br>
<input TYPE="text" NAME="aText" SIZE="80" VALUE="Scriptology">
<br>
<input TYPE="button" VALUE="insert at caret"
ONCLICK="insertAtCaret(this.form.aTextArea,
this.form.aText.value);"</form>
</body>
</html>
But I cannot the char's position
any ideas ?
I got the following problem :
I have a input text which holds the following value : hello
Now, when the user click on the first l, a window.alert pops up and
prints 3.
Question : Is it possible to get the cursor's position in an input text?
Note I said input text, not a textarea.
So far, I've found this :
<html>
<head>
<script>
function storeCaret (textEl) {
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
alert(caretPos.text.length - 1);
caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' '
? text + ' ' : text;
}
else
textEl.value = text;
}
</SCRIPT>
</head>
<body>
<form NAME="aForm">
<textarea NAME="aTextArea" ROWS="5" COLS="80" WRAP="soft"
ONSELECT="storeCaret(this);"
ONCLICK="storeCaret(this);"
ONKEYUP="storeCaret(this);"Kibology for all.
All for Kibology.
</textarea>
<br>
<input TYPE="text" NAME="aText" SIZE="80" VALUE="Scriptology">
<br>
<input TYPE="button" VALUE="insert at caret"
ONCLICK="insertAtCaret(this.form.aTextArea,
this.form.aText.value);"</form>
</body>
</html>
But I cannot the char's position
any ideas ?