473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text box cursor location

Frinavale
9,735 Recognized Expert Moderator Expert
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 28051
acoder
16,027 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Expert
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 New Member
[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 New Member
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator MVP
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
adrian74
1 New Member
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 Recognized Expert Moderator Expert
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
MRamaLakshmi
6 New Member
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 Recognized Expert Moderator MVP
Easy - check the source of the linked page.
Oct 30 '08 #13
MRamaLakshmi
6 New Member
Easy - check the source of the linked page.
the source is pointing to some external js file.
Oct 30 '08 #14
gits
5,390 Recognized Expert Moderator Expert
... 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 Recognized Expert Moderator MVP
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
MRamaLakshmi
6 New Member
... 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

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

Similar topics

1
1943
by: Ilann | last post by:
Hi, I need to get the text cursor position in a richedit Control, ie I want my ContextMenu to appear exactly where the user is typing text, Thanks
1
5966
by: Thilo Frank | last post by:
I want to add a flashing text cursor to my selfmade user control. Is there an existing class availible for this problem or do I have to write a thread, which flashs the cursor? THX for your help...
1
7340
by: Kidus Yared | last post by:
I am having trouble with the cursor in a richtextbox for windows forms. After incerting a character between words in a sentence, as so: this.richTextBox1.Text =...
0
1283
by: Peteroid | last post by:
I'm doing a managed C++.NET (2003) application where I use a Control::TextBox. When typing in text there is a cursor indicating the position of where typed in text will go next (typically, a...
4
1665
by: Sid S. | last post by:
How can I find the absolute cursor location? Where the mouse cursor is located with repect to the body. This way I can position an div tag where the mouse is, even if scrolled down farther on the...
2
1333
by: T Perkins | last post by:
is there a way to change the text cursor to something other that that I-beam thing using code. i have a user that cant see that well and would like to see where she is at in a text box. any help...
4
5049
by: Strahimir Antoljak | last post by:
I want to track the cursor location relative to a control on the form. I am aware of System.Windows.Forms.Cursor.Position which provides absolute cursor position relative to the screen. With right...
11
3290
idsanjeev
by: idsanjeev | last post by:
how can set cursor location everytime when page is open cursor goes to auto in input field and chake when click on submit lenth of input field
2
2932
by: =?Utf-8?B?cm9zc3RnYnI=?= | last post by:
My text cursor has changed from a vertical line to a large square black box. How do I change it back?
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.