input field focus

W

Will_uk

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
 
B

Bart Van der Donck

Will_uk said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top