472,139 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,139 software developers and data experts.

Text box cursor location

Frinavale
9,735 Expert Mod 8TB
I am just wondering if it is possible to determine where the cursor is located within an <input type='text'> element using JavaScript?

I'd like to write a snippet of code that will move to the focus to the next text box when the user has pressed the left arrow key and the cursor location has hit the text box's maxLength.

Likewise I'd like the focus to move to the previous text box when the user has pressed the right arrow key (or backspace) and the cursor location has hit the beginning of the text box.

I can't seem to find any property that indicates what the index value of where the cursor is located at within a text element.

Thanks for your help!
-Frinny
Apr 17 '07 #1
16 27858
acoder
16,027 Expert Mod 8TB
You can find the location using selection range properties. See this link (not exactly the solution, but you can work with it).
Apr 18 '07 #2
Frinavale
9,735 Expert Mod 8TB
You can find the location using selection range properties. See this link (not exactly the solution, but you can work with it).
I am pretty frustrated with this.
I'm finding it very hard to find useful JavaScript resources. W3C is usually where I gather all my information from but when it comes to this stuff it's not very helpful. I've tried your recommended links too but haven't been able to sort out my confusion.

I'm trying to understand the content of that link you sent me but I am confused.

Could you please give me some direction on what document.selection.createRange() is responsible for.

Or even what document.selection is.
I've tried looking up the document object but keep getting vague overview of it.

Thanks for your help!

-Frinny
Apr 18 '07 #3
jx2
228 100+
[HTML]<html><head>
<SCRIPT LANGUAGE="JAVASCRIPT"><!--
function show_coords(event)
{
x=event.clientX
y=event.clientY
document.getElementById("coords").innerHTML=("X coords: " + x + ", Y coords: " + y)
}
--></SCRIPT>

</HEAD>
<BODY BACKGROUND=red ">
<div id=coords></div><INPUT onmousemove="show_coords(event)">
</body></html>[/HTML]

i hope it helps
jx2
Apr 18 '07 #4
jx2
228 100+
lol
i vavent read it properly:-)
i thing you need to look here http://javascript.nwbox.com/cursor_position/
see source and be happy:-)
regards
jx2
Apr 18 '07 #5
acoder
16,027 Expert Mod 8TB
I am pretty frustrated with this.
I'm finding it very hard to find useful JavaScript resources. W3C is usually where I gather all my information from but when it comes to this stuff it's not very helpful. I've tried your recommended links too but haven't been able to sort out my confusion.

I'm trying to understand the content of that link you sent me but I am confused.

Could you please give me some direction on what document.selection.createRange() is responsible for.

Or even what document.selection is.
I've tried looking up the document object but keep getting vague overview of it.

Thanks for your help!

-Frinny
For information about selections, see this link.

Now if you can get the start position of this selection, you should get the cursor position (there's no simple way about it, unfortunately).

Also, read from the Mozilla docs about Selection and Range objects.
Apr 18 '07 #6
acoder
16,027 Expert Mod 8TB
lol
i vavent read it properly:-)
i thing you need to look here http://javascript.nwbox.com/cursor_position/
see source and be happy:-)
regards
jx2
Nice link - let's see if it's good enough for Frin.
Apr 18 '07 #7
Frinavale
9,735 Expert Mod 8TB
lol
i vavent read it properly:-)
i thing you need to look here http://javascript.nwbox.com/cursor_position/
see source and be happy:-)
regards
jx2

Beautiful! Thank you so much!
That's exactly what I would like to do....well just move the cursor back to the previous field once the cursor position reaches 0.

When I looked at the source I noticed that the JavaScript that controls this page is in an external file.

Gur
Apr 18 '07 #8
Frinavale
9,735 Expert Mod 8TB
I finally have a working solution!

<does a little victory dance>

Thank you guys for all your help!
If you hadn't have lead me to the selection stuff I would have never figured this out!


Cheers!!!

-Frinny
Apr 19 '07 #9
The cursor position control example is exactly what I need, but I need to do this on a content editable element.
Sep 19 '07 #10
Frinavale
9,735 Expert Mod 8TB
The cursor position control example is exactly what I need, but I need to do this on a content editable element.
What do you mean?
Could you describe your problem in more detail please?

-Frinny
Sep 19 '07 #11
Beautiful! Thank you so much!
That's exactly what I would like to do....well just move the cursor back to the previous field once the cursor position reaches 0.

When I looked at the source I noticed that the JavaScript that controls this page is in an external file.
Gur
Hi,

i am in a urgent need for this cursor position.
it exactly matches my requirement.
if you got the external js file for knowing the cursor position using javascript, please send it to me. its urgent.

thanks.
lakshmi.
Oct 30 '08 #12
acoder
16,027 Expert Mod 8TB
Easy - check the source of the linked page.
Oct 30 '08 #13
Easy - check the source of the linked page.
the source is pointing to some external js file.
Oct 30 '08 #14
gits
5,390 Expert Mod 4TB
... as acoder said ... its easy, when you look at the source then you see it has to be in the same path so you could just call it in your adress-bar with:

http://javascript.nwbox.com/cursor_position/cursor.js

it's a normal textfile and you should just see the JS-code in your browser ...

kind regards
Oct 30 '08 #15
acoder
16,027 Expert Mod 8TB
Yes, cursor.js which you can download, view or copy (as long as the license allows). Just type in cursor.js in the address bar to view the contents. If you have a debugger like Firebug (in Firefox), you can just select the file within the 'script' tab.
Oct 30 '08 #16
... as acoder said ... its easy, when you look at the source then you see it has to be in the same path so you could just call it in your adress-bar with:

http://javascript.nwbox.com/cursor_position/cursor.js

it's a normal textfile and you should just see the JS-code in your browser ...

kind regards
thanks for sending the js file.

i am very new to javascript, starting now.
thanks for the help.

well if you don't mind i need some help from you.
Oct 30 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ilann | last post: by
1 post views Thread by Thilo Frank | last post: by
reply views Thread by Peteroid | last post: by
4 posts views Thread by Sid S. | last post: by
2 posts views Thread by T Perkins | last post: by
4 posts views Thread by Strahimir Antoljak | last post: by
idsanjeev
11 posts views Thread by idsanjeev | last post: by
2 posts views Thread by =?Utf-8?B?cm9zc3RnYnI=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.