Set focus and cursor at end of text?

Discussion in 'ASP .Net' started by Olav Tollefsen, Jan 19, 2004.

  1. 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=\"javascript\">document.getElementById('" +
    ctrl.ID + "').focus()</SCRIPT>";

    RegisterStartupScript("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
    Olav Tollefsen, Jan 19, 2004
    #1
    1. Advertising

  2. Olav,

    Here we go:

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

    function SetEnd (TB)
    {
    if (TB.createTextRange)
    {
    var FieldRange = TB.createTextRange();
    FieldRange.moveStart('character', TB.value.length);
    FieldRange.collapse();
    FieldRange.select();
    }
    }

    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)">Existing 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=\"javascript\">document.getElementById('" +
    ctrl.ID + "').focus()</SCRIPT>";

    RegisterStartupScript("focus", s);
    }

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

    Good luck!

    Regards,

    Mohammad Samara.

    ICS (London) Ltd.


    "Olav Tollefsen" <> wrote in message
    news:...
    > 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=\"javascript\">document.getElementById('" +
    > ctrl.ID + "').focus()</SCRIPT>";
    >
    > RegisterStartupScript("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
    >
    >
    Mohammad A. Samara, Jan 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dmaziuk
    Replies:
    3
    Views:
    549
    Chris Gonnerman
    Jan 25, 2011
  2. ton

    set focus at end of text in textbox

    ton, Mar 17, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    408
  3. SibAndela
    Replies:
    1
    Views:
    178
    Jeff Cochran
    Sep 26, 2004
  4. duzuike
    Replies:
    2
    Views:
    126
    duzuike
    Aug 11, 2005
  5. Roger
    Replies:
    3
    Views:
    306
Loading...

Share This Page