Jump to content

Help with very simple JavaScript

ibbadib

I am a noob with software development in general so go easy on me :-).

Anyway I started taking the Lynda course for JavaScript Essentials.

 

I have been trying to make a text field prompt you to write your email by displaying "your email. Then onFocus the value should be set to "". Then again onBlur should set it to "your email".

But it simply won't do it. It just does nothing. I know it's a small issue but I feel so dumb not being able to do this. especially since I already made this work once, I just had to delete the project for some other reason.

And I really checked 100 times to see if my code was any different from the teacher's.

 

Appropreate (how to spell?) HTML:

E-mail: <input id="email" type="text" name="email" /><br>

 

JavaScript:

 
var emailField = document.getElementById("email");
 
emailField.onFocus = function() {
if (emailField.value == "your email") {
emailField = "";
   }
};
 
emailField.onBlur = function() {
if (emailField.value =="") {
emailField = "your email";
   }
};
 
 
BTW: I linked correctly to the JS file as I can make an alert with no problems.
Link to comment
Share on other sites

Link to post
Share on other sites

Something like this: http://jsfiddle.net/MMM5L/?

<input id="emailField" type="text" name="email" value="your email" />
var emailField = document.getElementById("emailField");emailField.onfocus = function() {    if (emailField.value == "your email") {        emailField.value = "";    }}; emailField.onblur = function() {    if (!emailField.value) {        emailField.value = "your email";    }};

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Something like this: http://jsfiddle.net/MMM5L/?

<input id="emailField" type="text" name="email" value="your email" />
var emailField = document.getElementById("emailField");emailField.onfocus = function() {    if (emailField.value == "your email") {        emailField.value = "";    }}; emailField.onblur = function() {    if (!emailField.value) {        emailField.value = "your email";    }};

Thanks it is working now:-). Although I have no clue to why it didn't in the first place...

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks it is working now:-). Although I have no clue to why it didn't in the first place...

Lets see; you were accessing the events by incorrect names see here, they should have been lower case. You were also not referencing .value in places where you were attempting to set the value. Lastly the !emailField.value is an optimization over == "". Inspecting the console output via your browsers development tools will likely be of benefit to you in the future.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

How I Learned to Stop Worrying and Love the Bomb - debugger is your best friend. Drop it in before failing line and inspect whats going on. Great for experimenting too.

Link to comment
Share on other sites

Link to post
Share on other sites

Lets see; you were accessing the events by incorrect names see here, they should have been lower case. You were also not referencing .value in places where you were attempting to set the value. Lastly the !emailField.value is an optimization over == "". Inspecting the console output via your browsers development tools will likely be of benefit to you in the future.

Wow thanks:-). And I did figure the .value thing out, but after I posted :D.

Link to comment
Share on other sites

Link to post
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

×