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

moving the cursor to the end of a textboxes text

Hi there,

I am using the following code to enter text into text box when it is
empty:

<input name="txt_url" type="text" class="form_text_300" id="txt_url"
onClick="if(this.value == ''){this.value = 'http://';}">

it works fine but when it enters the text - 'http://' - the cursor
jumps back to the start of the line in the text box. How do I then
make the cursor be placed at the end of the text. For example:

http://
here------------------'

http://
and not-------'

Cheers

Burnsy
Jul 23 '05 #1
2 1962
mr_burns wrote:
How do I then
make the cursor be placed at the end of the text.


Some helpfull functions:

function setSelectedTextRange(elm, selectionStart, selectionEnd) {
if (elm.setSelectionRange) {
elm.focus();
elm.setSelectionRange(selectionStart, selectionEnd);
}
else if (elm.createTextRange) {
var range = elm.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToEnd (elm) {
setSelectedTextRange(elm, elm.value.length, elm.value.length);
}
function setCaretToStart (elm) {
setSelectedTextRange(elm, 0, 0);
}
function setCaretToPos (elm, pos) {
setSelectedTextRange(elm, pos, pos);
}
Use it like this:
var iField = document.getElementById("inputfield");
iField.value = "foobar";
setCaretToPos(iField,3);
//or
setCaretToEnd(iField);

Or in your case:
<input name="txt_url" type="text" class="form_text_300" id="txt_url"
onClick="if(this.value == ''){this.value = 'http://';setCaretToEnd(this);}">

Daniel
Jul 23 '05 #2
bi******@yahoo.co.uk (mr_burns) wrote in message news:<65**************************@posting.google. com>...
Hi there,

I am using the following code to enter text into text box when it is
empty:

<input name="txt_url" type="text" class="form_text_300" id="txt_url"
onClick="if(this.value == ''){this.value = 'http://';}">

it works fine but when it enters the text - 'http://' - the cursor
jumps back to the start of the line in the text box. How do I then
make the cursor be placed at the end of the text. For example:

http://
here------------------'

http://
and not-------'

Cheers

Burnsy


Try:-

IE

var rRange=document.body.createTextRange();
rRange.moveToElementText("txt_url");
rRange.collapse(false);
rRange.select();

Mozilla

var nLength=txt_url.value.length;
txt_url.focus();
txt_url.setSelectionRange(nLength,nLength);
Julian
Jul 23 '05 #3

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

Similar topics

12
by: Kevin Blount | last post by:
I'm playing with dragable <DIV>s, being inspired by google.com/ig, where you can move items on the page around and have items you move over change position if necessary. I have 3 div's setup,...
4
by: erkki | last post by:
Hi, I have to move iSeries library to new DB2 database. Is there any easier way to do this moving operation than exporting all tables in iSeries into text files and then import them into DB2...
7
by: jcrouse | last post by:
I use the following three subs to click on a label and reposition it at runtime: Private Sub lblP1JoyUp_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)...
4
by: Brad Markisohn | last post by:
I'm using small text boxes to indicate specific states by altering the BackColor property. I also have a number of other controls on the form, but when I run the app I see the cursor flashing in...
3
by: Just Me | last post by:
If I move the mouse cursor over a control and stop moving I get a MouseHover event. If I then move the cursor while staying within the control and then stop moving I do not get another...
2
by: ThunderMusic | last post by:
Hi, I have a form in which there are many <asp:textbox> tags. When I run the form, we use tab to navigate across the textboxes, but one of our client noticed that when we navigate to some readonly...
2
Frinavale
by: Frinavale | last post by:
Hi there! I'm trying to write a simple JavaScript that will move the cursor to the next TextBox after 5 characters (or numbers) have been entered. I'm attempting to get this working in all...
3
by: Klaus Brune | last post by:
Hello all, When one tabs through a form (specifically, in Firefox), all the text in a field is automatically selected. What I'm looking for is a way to put a function (in onFocus perhaps)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.