473,657 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28876
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.selectionSta rt=="number") {
i=el.selectionS tart;
} else if(document.sel ection && el.createTextRa nge) {
sel=document.se lection;
if(sel){
r2=sel.createRa nge();
rng=el.createTe xtRange();
rng.setEndPoint ("EndToStart ", r2);
i=rng.text.leng th;
}
} else {
el.onkeyup=null ;
el.onclick=null ;
}

el.form.element s["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.tx t.value.length+ 1;
if (document.f.txt .createTextRang e){
theCaret = document.select ion.createRange ().duplicate();
while (theCaret.paren tElement()==doc ument.f.txt
&& theCaret.move(" character",1)== 1) --i;
}
return i==document.f.t xt.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
4896
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 write ten words in the text-area, then I move the cursor at the beginning of the second word and activate the event (for example, click on a button): is there a way to know that the cursor is at the beginning of the second word?
1
5080
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 box by 1 if my cursor is in front of the decimal point. If the cursor is after the decimal point, I want to increment the value by 0.1. How do I accomplish this? Is there a way to find out the cusror position in the control relative to decimal...
1
3853
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 TextBox ?? (relative to screen) I mean, i have a textbox that i write some text and i want to know the exact point location of the cursor at any time a character is displayed.
22
4498
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 that difficult, but to find the next line is more difficult. For exemple: if my cursor is on line 200, it has to start searching on line 201, and not on line 1. Anybody has any ideas?
1
4595
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 region). The image map is created in javascript, but doesn't set any 'cursor' style rules. The problem I'm having is I have two images embedded inside two divs. The outer div has style='... cursor:crosshair', but when I move over the image...
13
3330
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 Text property and concatenating it... Me.tb.Text &= newText ' Instead use Me.tb.AppendText(newText) ....ultimately setting the SelectionStart to the end of TextBox, the
4
4671
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 and write the fields into strings of rows, I want to be able to put field1 in row1/column position1, field2 in row1/column position10,.....etc. I've included the basic code to write a string of text. I understand the process of how to write the...
0
1908
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 gtk_editable_set_position (GTK_EDITABLE((GtkEntry*)user_data), 4);
1
9177
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 part: When our people click on a blank MaskedTextBox field, the cursor does not automatically go to the first "non-filled" mask value. For Example: Take the MaskedTextBox "___-___-___". When our people click the Text area, their cursor may stop...
0
2809
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 Form. A user can drag and drop a text(Usually from a tree node its name would be appended on drag and drop) in this control. Here user can put cursor any where inside this texteditor control and he can drag and drop text into it. Eg: If text editor...
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8516
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7353
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
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 we have to send another system

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.