473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 28044
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

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

Similar topics

1
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.