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

Home Posts Topics Members FAQ

Set focus and cursor at end of text?

I use this code to set focus to a textbox when I load a page:

private void SetFocus(System .Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"java script\">docume nt.getElementBy Id('" +
ctrl.ID + "').focus() </SCRIPT>";

RegisterStartup Script("focus", s);
}

I would also like the cursor to be positioned at the end of the text field.
Using only the above code, the cursor is positioned at the beginning of the
textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

Olav
Nov 18 '05 #1
1 9729
Olav,

Here we go:

Create a new function in your <head> tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextR ange)
{
var FieldRange = TB.createTextRa nge();
FieldRange.move Start('characte r', TB.value.length );
FieldRange.coll apse();
FieldRange.sele ct();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

<asp:textbox id="TextBox1" runat="server" Width="65px"
onfocus="SetEnd (this)">Existin g text</asp:textbox>

Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System .Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"java script\">docume nt.getElementBy Id('" +
ctrl.ID + "').focus() </SCRIPT>";

RegisterStartup Script("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load .....( <body onload="SetFocu s()"> )

Good luck!

Regards,

Mohammad Samara.

ICS (London) Ltd.
"Olav Tollefsen" <x@y.com> wrote in message
news:ux******** ******@TK2MSFTN GP11.phx.gbl...
I use this code to set focus to a textbox when I load a page:

private void SetFocus(System .Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"java script\">docume nt.getElementBy Id('" +
ctrl.ID + "').focus() </SCRIPT>";

RegisterStartup Script("focus", s);
}

I would also like the cursor to be positioned at the end of the text field. Using only the above code, the cursor is positioned at the beginning of the textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

Olav

Nov 18 '05 #2

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

Similar topics

11
4473
by: JCO | last post by:
I have a text box, on my form that I cant get the focus on. I'm using it as shown: <head> <body onload=document.getPassword.txtPasswordName.focus()> </head> Where getPassword is the form & txtPasswordName is the text box
4
2395
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something problematic. Given the following code: <HTML> <HEAD> </HEAD> <BODY> <FORM>
20
12296
by: Arne | last post by:
During testing <div style="overflow:auto;"> in CSS I noticed the mousewheel would work in Mozilla only after I made a <a href="#">some text</a> link and clicked on that, within the div. It appears as if Mozilla needs to have the focus set on that div in order for the mousewheel to work. That's all that link does. The mousewheel works perfectly in IE without the link. It scrolls the div even if there is a scrollbar on the page. Is...
3
2619
by: Dai Ba Wong | last post by:
Hi: Currently I am having a problem with my webpage. My page consist of two frames, one consist of input text field and the other contains link for different pop-up windows. The problem follows: 1. At the beginning, focus is placed on an input text field of the first frame (so there's a blinking cursor on such field). 2. Then users click on a link of the second frame (thus focus is set
5
19260
by: orahm | last post by:
I have 2 textboxes and one button and I don't know why my code doesn't work. All I'm trying to do is write a number 1 into the textbox with the focus. so if the user puts the cursor in textBox1 and pushes the button the 1 should go into that box. If the cursor is in the other one the 1 should go there. It seems simple but doesn't work. Can anyone give me any hints? Here is the code: private void button1_Click(object sender,...
16
3633
by: Stuart | last post by:
I have a page where I utilise the left and right cursor keys to change an image, also on this page is a groups of radio buttons, unfortunately when a radio button is clicked the focus for the cursor keys goes to the radio buttons and any subsequent cursor key presses scroll through the radio buttons. How do I take focus away from the radio buttons after they have been clicked ?
2
3872
by: Rey | last post by:
Howdy all. Using visual web developer (VB) on xp pro box. My problem with with a web form that on accessing the calendar control causes a postback that moves the cursor back to the txtFirstName field. Form uses the Page_LoadComplete in order to access the last employeeID # in a dataview which is populated by an sqlDataObject querying an SQL Server dbs.
11
5343
by: karen987 | last post by:
I have a web page which used to work fine until i added a rich text editor. the pages are in ASP and it is a news weblog, with a comments section where people click the headline of a comment and it opens into a pop up page ccalled "comment_view.asp" which has a reply button. If anyone wants to reply to the message, they click reply and it takes them to the parent page (view.asp) where there is a form for them to fill in,and submit the...
11
1805
by: sheldonlg | last post by:
I have a problem with not seeing the current focus while in a particular screen in a my app. The focus does not show for a type=text or textarea. I have tried to reproduce this problem in a test application, but have not been successful is doing that. The actual app has a screen which is displayed on a template and the contents are the return from an AJAX call. In that content there is a hidden block. Clicking on a button in the...
0
8392
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
8305
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
8823
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...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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,...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
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...
1
2726
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
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.