Relegence
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
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
do u want to insert at cursor position?
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.
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.createR ange();
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.getEl ementById( 'txt'));">
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function findcursor(myField) {
if (document.selection) {
var sel = document.selection.createR
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.getEl
</BODY>
</HTML>
ASKER
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
so suppose
var temp = sel.text
var pos1 = temp.charAt(0) //starting
and
var pos2 = temp.charAt(temp.length-1)
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works.
Thank you very much for your help :-)
Thank you very much for your help :-)
sorry for the mess up..
glad u got to work!
glad u got to work!