• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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
0
Relegence
Asked:
Relegence
  • 5
  • 4
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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
 
RelegenceAuthor Commented:
It works.
Thank you very much for your help :-)
0
 
archrajanCommented:
sorry for the mess up..
glad u got to work!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now