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
RelegenceAsked:
Who is Participating?
 
archrajanCommented:
Ooops.. sorrry
check this:
<script>
function findcursor(myField) {

if (document.selection) {

var sel = document.selection.createRange();
alert(sel.text);
var temp = sel.text
var pos = myField.value.indexOf(temp);
alert(pos);
var pos1 = pos+(temp.length-1)
alert(pos);


}


}

</script>
0
 
archrajanCommented:
do u want to insert at cursor position?
0
 
RelegenceAuthor Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
archrajanCommented:
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>
0
 
RelegenceAuthor Commented:
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
0
 
archrajanCommented:
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
0
 
RelegenceAuthor Commented:
I need to find the indexOf the start position and the end position and here i get the first letter and last letter
0
 
RelegenceAuthor Commented:
It works.
Thank you very much for your help :-)
0
 
archrajanCommented:
sorry for the mess up..
glad u got to work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.