Click to See Complete Forum and Search --> : Finding the cusor position in a text area in IE


DJRobThaMan
12-12-2005, 10:39 PM
Hi,
I'm trying to figure out the curser position in a textarea in IE whether there's a selection or not. The selectionStart and selectionEnd properties work perfect in Mozilla (If only microsoft could make it that easy).

Here's the code I'm trying to make work (tried lots of variations already):


var msg;

function setbox(x)
{

msg = document.getElementById(x);

}

function lookaround()
{

if(!msg)
{
alert("no focus");
}
else
{
if(document.selection)
{
msg.focus();
alert(document.selection.createRange().text);
}
else
{
alert(msg.selectionStart);
}
}

}


This code I have was a test to see that the text I selected from the textarea actually went into the range correctly but the range always seems to be empty. Is this because I'm calling this function from an onClick event of a <td> element and so when I click the <Td> the focus goes away from the textarea and when the focus returns, because of the focus() method in the function, the text that was selected now is not? (I don't see why that should really happen though seeing as in mozilla I call it in the same way and the alerts show the correct values for the cursor positions). Does anybody know of any other way to figure out what part of the text in a textarea is selected and extract it to manipulate it, or just simply the equivalent in IE of the selectionStart and selectionEnd values?

Thanks a lot,
Douglas

DJRobThaMan
12-13-2005, 01:31 AM
Ok, so that is exactly what the problem is. When it loses focus, the selection is lost along with it (I called the function when the mouse leaves the text area and the selection text came up perfectly.

Does anybody know of a way to get the cursor position inside the textarea where I can click an element outside of the said area and get the info. Any nudges in the right direction would be greatly appreciated.

Thanks,
Douglas

Kravvitz
12-13-2005, 01:43 AM
http://www.webdeveloper.com/forum/showthread.php?t=81849

DJRobThaMan
12-13-2005, 02:27 PM
Thanks man,

That link helped, the range doesn't include the actual selection (i guess because the textarea loses focus) but I think creating the range when the onBlur event fires would fix that. Gots to go test it now.

Douglas