473,421 Members | 1,638 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,421 software developers and data experts.

cursor position in text box

AP
I am trying to find the integer value for the cursor position in a text box,
in particular I want to find out if I am at the end of some text that I
typed or at the beginning.

I am using IE 6.

Thanks,

Andy
Jul 23 '05 #1
2 28855
AP wrote:
I am trying to find the integer value for the cursor position in a text box,
in particular I want to find out if I am at the end of some text that I
typed or at the beginning.


The following will give you the position of the caret, the rest is a
matter of substring-ing the control value and determine whether the
position is the one required.

Few browsers support ranges, though; you can expect it to work on IE and
Mozilla, but probably no other agents yet. Limitations in supporting
browsers are to be sought in the function triggering (can you see
situations where the function won't be triggered while it should be?).
<form>
<input type="text" onkeyup="s(this)" onclick="s(this)">
<input type="text" name="position">
</form>

<script type="text/javascript">
function s(el){
var sel, rng, r2, i=-1;

if(typeof el.selectionStart=="number") {
i=el.selectionStart;
} else if(document.selection && el.createTextRange) {
sel=document.selection;
if(sel){
r2=sel.createRange();
rng=el.createTextRange();
rng.setEndPoint("EndToStart", r2);
i=rng.text.length;
}
} else {
el.onkeyup=null;
el.onclick=null;
}

el.form.elements["position"].value=i;
}
</script>
Jul 23 '05 #2
Ivo
"AP" wrote
I am trying to find the integer value for the cursor position in a text box, in particular I want to find out if I am at the end of some text that I
typed or at the beginning.

I am using IE 6.


That 's fine, but the question is what your users use. Incidentally, the
following code is IE-only:

function caretPos(){
var i=document.f.txt.value.length+1;
if (document.f.txt.createTextRange){
theCaret = document.selection.createRange().duplicate();
while (theCaret.parentElement()==document.f.txt
&& theCaret.move("character",1)==1) --i;
}
return i==document.f.txt.value.length+1?-1:i;
}
// assuming document.f.txt is your textbox.

If the cursor is not in the textbox, the function returns a value of -1,
similar to the indexOf() method for strings.
HTH
Ivo
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PeP | last post by:
Good morning, I have a form containing a text-area, I'd like to know if it exists a function that, when I activate an event, returns the position of the cursor in the text-area. For example, I...
1
by: Tantra Veda | last post by:
Hello C# gurus, I have a question about finding cursor position in NumericUpDown control. On my form I have a numericUpDown control with 2 decimal places. I want to increment value in the numeric...
1
by: objectref | last post by:
Hi to all, we have the MousePosition property that we can get the Point of the position of the mouse cursor on the screen, but is it there a way to get the respective Point of a cursor in a...
22
by: DraguVaso | last post by:
Hi, For my application I need the following behavior: When I press F4 the cursor has to move to the next line in my multiline textbox which begins with "0". Finding lines starting with 0 isn't...
1
by: Andy Baxter | last post by:
hello, I'm writing a panoramic image viewer in javascript. When the mouse is over the image, I want it to be a crosshair (over most of the image), or a hand/pointer (when it's over an image map...
13
by: WALDO | last post by:
I have a .Net TextBox (TextBoxBase, really) in which I am appending about 20 lines of text per second. I use the AppendText() method to accomplish this. This is a great substitute for taking the...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
0
by: jnakam | last post by:
Hi, Im trying to get the gtk_editable_set_position to work. I have a gtkEntry text box and i first enter "12378978987913". When I press '4' I expect the cursor to move to the fourth position...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
We have some dumb and lazy people here, so I need help. To fix the dumb part: We have placed MaskedTextBoxes on the forms so they will stop entering the information incorrectly. Now the lazy...
0
by: nagarjunt | last post by:
Hi, This is nag. I have a problem with infragistics7.1 UltraTextEditor control. I am using C# code in windows form. I have UltraTextEditor control(With multi line enabled) in my Windows...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.