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?

  • No druapl way to achieve this. Need to use some javascript. – niksmac May 8 '12 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 = ''}", ), – generalconsensus May 8 '12 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.. – generalconsensus May 8 '12 at 5:16
  • Yeah #attributes its the better way. Nice finding. Also post answer. – niksmac May 8 '12 at 5:34
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'),
  ),   
);
  • This is a way better answer since it uses the built in Ajax and just simply works! – shaneonabike May 6 '15 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 = ''}", 
  ),   
);
  • But that's not the drupal way! – Coomie Oct 15 '12 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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