Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting cursor position

Status
Not open for further replies.

pazan

Programmer
Mar 10, 2005
6
CH
Hello,

I'm searching a solution to get the position of the cursor in a text field. Can anyone help me?

So if We have a bit of code like this:

<HTML>
<SCRIPT language = "javascript">
function getCursorPos(elem){
...
}
</SCRIPT>
<BODY>
<INPUT type = "text" id = "textField">
<INPUT type = "button" value = "Display cursor position" onClick = "javascript:alert(getCursorPos(document.getElementById('textField')));">
</BODY>
</HTML>

Does someone know what to write in place of the getCursorPos function...
 

AFAIK, once the input field no longer has focus (i.e. as soon as you click the button), the caret will no longer be in that field, and so there will be no cursor position to detect.

You should remove the "javascript:" from the onclick attribute anyway - it's not needed.

Hope this helps,
Dan


The answers you get are only as good as the information you give!

 
Ok,

I'm sorry for this stupid introduction. That was just an example. So just imagine a function that gives the cursor position IF THE FIELD HAS THE FOCUS (forget the button) and a negative value else (for example -1). Hope this is clearer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top