input field focus

Discussion in 'Javascript' started by Will_uk, Feb 28, 2007.

  1. Will_uk

    Will_uk Guest

    I am trying to return focus to a form input field.

    If i use <form>.<field>.focus() the focus returns to the field but if
    the field already contains a value it selects it (blacks it out).

    Is it possible to focus on a input field but rather than selecting any
    already inputted text, putting the cursor to the end of the field.

    Cheers
    Will_uk, Feb 28, 2007
    #1
    1. Advertising

  2. Will_uk wrote:

    > I am trying to return focus to a form input field.
    > If i use <form>.<field>.focus() the focus returns
    > to the field but if the field already contains a
    > value it selects it (blacks it out).
    >
    > Is it possible to focus on a input field but rather
    > than selecting any already inputted text, putting
    > the cursor to the end of the field.


    This should do the trick:

    <form>
    <input type="text" name="t" value="123">
    <input type="button" value="click" onClick="
    document.forms[0].t.focus()
    var V = document.forms[0].t.value
    document.forms[0].t.value = ''
    document.forms[0].t.value = V
    var L = (document.forms[0].t.value).length
    if (document.forms[0].t.setSelectionRange) {
    document.forms[0].t.setSelectionRange(L, L)
    }
    else if (document.forms[0].t.createTextRange) {
    var range = document.forms[0].t.createTextRange()
    range.collapse(true)
    range.moveEnd('character', L)
    range.moveStart('character', L)
    range.select();
    }
    ">
    </form>

    Hope this helps,

    --
    Bart
    Bart Van der Donck, Feb 28, 2007
    #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. Adrian Parker

    Input field focus after postback - v1.1

    Adrian Parker, Mar 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    Steven Cheng[MSFT]
    Mar 10, 2006
  2. =?ISO-8859-15?Q?Josef_Bl=F6sl?=
    Replies:
    1
    Views:
    25,774
  3. leiko

    focus on a input field

    leiko, Feb 25, 2004, in forum: Javascript
    Replies:
    3
    Views:
    97
    McKirahan
    Feb 26, 2004
  4. Rune Runnestø
    Replies:
    4
    Views:
    172
    Thomas 'PointedEars' Lahn
    May 8, 2005
  5. Roger
    Replies:
    3
    Views:
    299
Loading...

Share This Page