Jump to content

onblur and onfocus help!


RawrRawrRob

Recommended Posts

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 by RawrRawrRob
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...