2

My objective is to automatically remove the default values within the form textfields upon either being selected/clicked on. Not really sure how to at the moment. Any suggestions?

4
  • No druapl way to achieve this. Need to use some javascript.
    – niksmac
    May 8, 2012 at 4:26
  • I learned that you should tie this to attributes as I listed below, however it is not working as intended. Thanks for the advice@Nikhil $form['Package']['FirstName'] = array( '#default_value' => t('First Name'), '#type' => 'textfield', '#maxlength'=> 255, '#required' => TRUE, '#attributes' => array( '#onblur' => "if (this.value == '') {this.value = 'First Name'}", '#onfocus' => "if (this.value == 'First Name') {this.value = ''}", ), May 8, 2012 at 5:09
  • Fixed: The correct code for erasing the default values in a textfield, etc. is: $form['Package']['FirstName'] = array( '#default_value' => t('First Name'), '#type' => 'textfield', '#maxlength'=> 255, '#required' => TRUE, '#attributes' => array( 'onblur' => "if (this.value == '') {this.value = 'First Name'}", 'onfocus' => "if (this.value == 'First Name') {this.value = ''}", ), ); Don't forget to remove the # call as the array attributes are additions not Drupal specific options.. May 8, 2012 at 5:16
  • Yeah #attributes its the better way. Nice finding. Also post answer.
    – niksmac
    May 8, 2012 at 5:34

2 Answers 2

2

What do you mean for "default value"? Do you need to save this value or just to use it as a placeholder?

In this case you can obtain the goal without javascript:

$form['Package']['FirstName'] = array(
  '#type' => 'textfield',
  '#maxlength'=> 255,
  '#required' => TRUE,
  '#attributes' => array(
    'placeholder' => t('First Name'),
  ),   
);
1
  • This is a way better answer since it uses the built in Ajax and just simply works! May 6, 2015 at 19:07
1

The correct code for erasing the default values in a textfield, etc. is:

$form['Package']['FirstName'] = array(
  '#default_value' => t('First Name'),
  '#type' => 'textfield',
  '#maxlength'=> 255,
  '#required' => TRUE,
  '#attributes' => array(
    'onblur' => "if (this.value == '') {this.value = 'First Name'}",
    'onfocus' => "if (this.value == 'First Name') {this.value = ''}", 
  ),   
);
1
  • But that's not the drupal way!
    – Coomie
    Oct 15, 2012 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.