Form field’s default value using Mootools

Sometimes it’s needed to put default value for input, textarea field’s in form. If field’s focusd, default value need to be vanished and if no value provided by user and field de-focused, default value need to be back again.


Using javascript, we can do this very easily with inline code but that is very messy. Mootools has very effective selectors class to handle that very easily. Just use the following codes, you don’t have to write anything in the field’s.

window.addEvent('domready', function(){

$(document.body).getElements('input[type=text],textarea').addEvents({

'focus' : function(){
if (this.get('value') == this.defaultValue)
{
this.set('value', '');
}
},
'blur' : function(){
if (this.get('value') == '')
{
this.set('value', (this.defaultValue));
}
}
});
});

This is pretty simple!

Advertisements

4 thoughts on “Form field’s default value using Mootools

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