It's All About Me

make them benefit

Form field’s default value using Mootools

with one comment

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!

Written by kodegeek

June 20, 2009 at 4:10 am

Posted in Javascript, Mootools

Tagged with

One Response

Subscribe to comments with RSS.

  1. Wao, nice post

    javed

    July 26, 2009 at 10:20 pm


Leave a Reply