Yvil Posted June 1, 2009 Report Share Posted June 1, 2009 (edited) Hey everybody,I've got this form input script that (should) change the input value to 'username' when the value is nothing: <form><input type="text" name="un" value="username" onblur="if (this.value = '', this.value = 'username');" onfocus="if (this.value = 'username', this.value = '');" /></form> It all works for the "onfocus" part, but the "onblur" part doesn't work: it automaticly changes the value to 'username', no matter what the current value is.Does anyone know why this could be?Thanks in advance,Yvil Edited June 1, 2009 by Yvil Link to comment Share on other sites More sharing options...
jeffman Posted June 1, 2009 Report Share Posted June 1, 2009 (edited) Your statements don't do what you think they do.1. In the conditional part of your if statement, you need the comparison operator ( == ) not the assignment operator ( = ).2. Use the correct syntax for the if statement: if (this.value == '') {this.value = 'username';} Notice where I closed the parentheses and where I put the braces. Notice that there is no comma. The comma is a real operator, but it is not used in simple if statements.You'll need to correct the onfocus part also. If it seems to work now, it can only be because you haven't tested it thoroughly. Edited June 1, 2009 by Deirdre's Dad Link to comment Share on other sites More sharing options...
Yvil Posted June 1, 2009 Author Report Share Posted June 1, 2009 Ah, I see.I always mess up the syntax for JavaScript.Lets hope I will remember it now ;-).Thanks Deidre's Dad! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now