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