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?
2 Answers
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! May 6, 2015 at 19:07
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 = ''}",
),
);
$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 = ''}", ),