RawrRawrRob Posted March 30, 2013 Share Posted March 30, 2013 (edited) Hey everyone, I'm trying to create a simple form where a user enters their name. I have the text field with an onfocus function that creates a div next to the field telling the user to enter their name. Now, with the onblur function, if the user enters nothing, using innerHTML the div will tell them again to enter their name. If they enter any text, the innerHTML in the div will change and say thank you. Here's my code so far, I feel like I have done it right, but it doesn't seem to work the way I want it to. Any help to point me in the right direction is greatly appreciated window.onload = apply //this function adds onfocus and onblur to the name fieldfunction apply(){ //onfocus to tell the user to enter their namedocument.querySelector("#firstname").onfocus = instructionsFirstName; //onblur to check if they entered their name or notdocument.querySelector("#firstname").onblur = checkFirstName;} //onfocus instructionsfunction instructionsFirstName(){ document.querySelector("#firstNameInstructions").innerHTML = "Type your first name here";} //onblur function to check if user entered anything or notfunction checkFirstName(){ var error = ""; if(error == ""){ document.querySelector("#firstNameInstructions").innerHTML = "Please type your first name!"; }else{document.querySelector("#firstNameInstructions").innerHTML = "thank you";}} Edited March 30, 2013 by RawrRawrRob Link to comment Share on other sites More sharing options...
jeffman Posted March 31, 2013 Share Posted March 31, 2013 (edited) Obviously, this is a problem: var error = ""; if(error == "") Edited March 31, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
RawrRawrRob Posted March 31, 2013 Author Share Posted March 31, 2013 (edited) Obviously, this is a problem: var error = ""; if(error == "") Hmmm ya, I thought it might have something to do with that. How would I fix this? Edited March 31, 2013 by RawrRawrRob Link to comment Share on other sites More sharing options...
jeffman Posted March 31, 2013 Share Posted March 31, 2013 You probably want to check the value (not the innerHTML) of "#firstname". If it == "", put up your error message. 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