Link to home
Start Free TrialLog in
Avatar of Relegence
RelegenceFlag for Israel

asked on

Finding the index within a textbox when onmousedown is fired

Hello,

I would like to find the index within a textbox where the mouse button was pressed and the event "onmousedown" was fired.

Thank you,
Dana
Avatar of archrajan
archrajan

do u want to insert at cursor position?
Avatar of Relegence

ASKER

The user has to mark a part of the string and i want to find the start position and the end position.
i want to find the cursor position onmousedown and onmouseup.
Try this:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function findcursor(myField) {

if (document.selection) {

var sel = document.selection.createRange();
alert(sel.text);
}

else if (myField.selectionStart || myField.selectionStart == 0) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
alert(startPos);
alert(endPos);
}
}
</script>


</HEAD>

<BODY>
<textarea id =" txt"></textarea>
<input type = "button" value = "find" onclick = "findcursor(document.getElementById('txt'));">
</BODY>
</HTML>
i can't get the start and end positions. i am trying to mark a part of the text and press the button  but can't get the positions, only the selected text
yeah the selectedtext's first letter and end letter would give u the position isnt it?

so suppose

var temp = sel.text

var pos1 = temp.charAt(0) //starting
and
var pos2 = temp.charAt(temp.length-1)//ending
I need to find the indexOf the start position and the end position and here i get the first letter and last letter
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works.
Thank you very much for your help :-)
sorry for the mess up..
glad u got to work!