...

View Full Version : how do you get cursor position?



kwhubby
01-08-2004, 08:27 PM
Hi, I forget how you get the cursor position in a text field.
Could somebody tell me how you do this please?

Willy Duitt
01-08-2004, 09:19 PM
I do not know if this is what you are looking for.
But, it may help :)

<html>
<head>
<script type="text/javascript">
function storeCaret(ftext){
if (ftext.createTextRange){
ftext.caretPos = document.selection.createRange().duplicate();
}
}

function insertsmilie(smilieface){
var t = document.f.t;
if (t.createTextRange && t.caretPos){
var caretPos = t.caretPos;
caretPos.text = smilieface;
t.focus();
}
else{
t.value+=smilieface;
t.focus();
}
}
</script>
<body onload="document.f.t.focus()">
<form name=f>
<textarea name=t cols="44" rows="15" onselect="storeCaret(this);"onclick="storeCaret(this);"
onkeyup="storeCaret(this);" onchange="storeCaret(this);"></textarea><br>
<button onclick="insertsmilie('8)');">Insert Smilie</button><br>
</form>
</body>
</html>

.....Willy

kwhubby
01-08-2004, 11:45 PM
thanks!, that is sortov what I am looking for!, although it does not work properly in netscape: one reason being that its using <button> instead of <input type="button">, and the other Is that in netscape it doesnt "insert" it simply adds at the end.

Although what i am doing is primarily for IE, id like to know how to do it for netscape 6+

jkd
01-09-2004, 01:33 AM
function insert(text, textarea) {
textarea.value = textarea.value.substr(0, textarea.selectionStart) + text + textarea.value.substr(text.length+textarea.selectionStart);
}

kwhubby
01-13-2004, 06:02 AM
sorry for the delay between for my reply. I tried what u said to do jkd but It inserts the text in the beggining of the textarea- I make it alert textarea.selectionStart tells me that it is undifined. Whats going on here, how do i get the cursor position for a textarea in moz?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum