How To : Simple javascript Subscription Form

Do you notice the nifty email subscription box on the sidebar. If you are a feed reader, you might want to click-through to check out. In this simple tutorial, I’ll teach you how to create this simple form using javascript.

1) First, here is the basic HTML code for an input box and a subscribe button :


2) Then, we have to add the javascript bit to make the text field blank when a user clicks inside it. For this, we make use of the onClick parameter


The if statement is pretty much self explanatory. On clicking, if the value inside the text field is “Enter Email Address…”, we should change its value to blank.

if(this.value == 'Enter Email Address..')
{
	this.value = '';
}

3) Now, that form is almost complete. We just need to add one more thing. When the user doesn’t enter any value and clicks outside the text field, we should bring back the “Enter Email Address…”. For this, we make use of the onBlur parameter.


This is also the same as the other statement. On blur (clicking somewhere outside), ifthe value of the text field is blank, we have to change it to “Enter Email Address…”

if(this.value == '')
{
	this.value = 'Enter Email Address..';
}

Thats it. Now, you have a cool form for email subscriptions (of course, this can be used for any other form).

6 Responses to “How To : Simple javascript Subscription Form”

Thanks that is really helpful. Do you have anything for validating email addresses using JavaScript?

Hari says:

Ya, i’ll post it in today’s “weekly wrap-up” post…

[...] Design Essex asked “Do you have anything for validating email addresses using JavaScript?” There are many [...]

Fun Director says:

Can you tell me who did your layout? I have been looking for one kind of like yours. Thank you.

Jodie says:

This worked amazingly well! Thank you! I am having one issue though. The form doesn’t go blank when clicked. I have copied the code exactly and I am not sure what else it could be. Any ideas? I would loooove for it work like it should.

Hari says:

Hey Jodie,
I checked your source code. There are small errors in it. If you check it, you’ll find sequence of characters starting with & and ending with ;

You’ll have to replace them with the actual characters. That is, instead of copying and pasting this code, just see this code and type it out. Everything will work fine then.