Forum Moderators: open

Message Too Old, No Replies

Make a cursor go to a textbox

         

andrewsmd

4:10 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if there is straight HTML that can do this or if I need JavaScript but, can how do I make the cursor start in a a login textbox. What I mean is, when a user opens my page and they have to login, I want the cursor already be in the username textbox. Is there something I can define within the textbox itself, or do I have to use JS. If so, what is the JS. Thanks

pageoneresults

4:13 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a page open now where we use this piece of JS to put the cursor at the Password field if there is an email passed or put them at the Email field if no email is passed. :)

<script type="text/javascript">
window.onload = function init() {
if (document.login.Email.value == '') {
document.login.Email.focus();
} else {
document.login.Password.focus();
}
}
</script>

<table>
<tr>
<td>Email Address:</td>
<td><input name="Email" size="25" type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="Password" size="25" type="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Login"></td>
</tr>
</table>

DRIVE-BY WARNING: I just happened to be driving by and saw your post. I am NOT an expert or even a novice in JS. But, I can understand some of what I'm looking at and my Cut and Paste routines are second to none. :)

rocknbil

3:30 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Supplement one: An alternate (standard, uses DOM?) method is to use documentGetElementById() to access the form element:

.....
if (document.getElementById('Email')) {
document.getElementById('Email').focus();
} .....
....<input name="Email" id="Email" size="25" type="text">......

Supplement two: be sure this is doing what your users want and not what you think they want, it's #1 on my top ten list [webmasterworld.com]. :-)

The reason being, onload often doesn't execute until the entire page loads. The fields may load before it executes and they may already be in the password field . . . then focus switches to the Email field and an continues to input the password into the email field. Maddening.