views:

26

answers:

1

Here is my javascript code for a cursor focus function to go to username if it is blank on a form call "login".

<script type = "text/javascript">
if (document.forms.login.user.value == "")
(
    document.forms.login.user.focus();
)   
else
(
    document.forms.login.password.focus();
)


Do I need to add anything to my form? Here it is.

<form action="form.php" method="post" name="login">
<label for="user"><b>Username:</b></label> <input name="user" type="text" id="user" size="20"/><br/>
<label for="password"><b>Password:</b></label> <input name="password" type="password" id="password" size="20"/><br/>
<input type="hidden" name="action" value="login"/><br/>
<input type="submit" id="submit" value="Login"/>
</form>
A: 

This works for me. (Removed the parentheses around the actual if and else sections.)

if (document.forms.login.user.value == "")
    document.forms.login.user.focus();
else
    document.forms.login.password.focus();

Working Example

http://jsfiddle.net/S8bRL/

Robert
thanks! I actually got it to work by using "{}" instead of "()"
tim
Yeah, the `{}` are optional if you're only running one command after the `if/else`, but are required if you're running multiple after the `if/else`.
Robert
oh ok gotcha..thanks
tim
@tim: that is because `()` is improper syntax
vol7ron