View Full Version : how do you get cursor position?

01-08-2004, 09: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, 10:19 PM
I do not know if this is what you are looking for.
But, it may help :)

<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;
<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>


01-09-2004, 12:45 AM
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+

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

01-13-2004, 07: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?