Add onfocus/onblur to drupal custom form fields

When you are wirting a custom form using Form API then if you want to show some text on text field / text area as disappeared when clicking in that box.Then basically you need to use onfocus / onblur events occurs when an element gets focus.

So you need to add default value and then you need to add a attribute like

<?php
'#default_value' => 'Status Message',
    
'#attributes' => array( 'onblur' => "if (this.value == '') {this.value = 'Status Message'}", 'onfocus' => "if (this.value == 'Status Message') {this.value = ''}", )
?>

Find the same code in below example

<?php
function my_module_form_elements($form_state) {
 
$form['status'] = array(
   
'#title' => t(''),
   
'#type' => 'textfield',
   
'#size' => 110,
   
'#default_value' => 'Status Message',
    
'#attributes' => array( 'onblur' => "if (this.value == '') {this.value = 'Status Message'}", 'onfocus' => "if (this.value == 'Status Message') {this.value = ''}", ),
  );

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Post'),
   
'#submit' => array('my_module_form_submit'),
  );
  return
$form;
}
?>
Here my form cantains a text field and having default value as 'Status Message'.So that I have added the onblur/onfocus when an element gets focus/onblur.