little JavaScript blur and focus trick for forms

This is just a clean way of running your text/password fields cleanly, without having to fill your pages with JS BS. Just put the following code between your <head>....</head> tags:

<script type="text/javascript">
<!--
function myFocus(element) {
if (element.value == element.defaultValue) {
element.value = '';
}
}
function myBlur(element) {
if (element.value == '') {
element.value = element.defaultValue;
}
}
-->
</script>

and then the following in the text or password declarations:

... onfocus="myFocus(this);" onblur="myBlur(this);" ...(dots are to be left out)

What this does for any field which has these applied to it is, populate them with their “value” on page load; but then empty them when they are clicked within, and repopulate them with their original value if nothing has been entered in them. This simplifies this re-filling of fields, as the original value is irrelevant to the specific implementation of the method, and is retrieved from the applicable field at time of use.

Advertisements

About Cameron
I'm a final year Computer Science/Information Systems major. Already finished my BA in Politics/Philosophy. I do web and software freelance on the side, while I finish studying. Hoping to be self-employed by the end of my degree, otherwise off into the real-world I go....

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: