Jump to content

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...