1

Similar to how the search box on many sites has a default value of "search", how do I give a select list a default value without making that value a valid option in the list of options. See example below:

image showing default value as instruction

2 Answers 2

2

For adding text to your input field, write a custom module and place the code in there

function yourmodule_form_alter(&$form, &$form_state, $form_id){
    if($form_id == "your_form"){
     $form['some_element']['#attributes']['placeholder'] = t('Select Category');
}
1
  • isn't placeholder an HTML5 attribute?
    – frazras
    Sep 12, 2013 at 16:47
2

you are looking for #empty_value attribute

If #required is TRUE and there is no #default_value, an empty option is added to the select control to force the user to make an active choice.

If #empty_value or #empty_option is set and #required is FALSE (default), an empty option is added to the select control, allowing the user to choose nothing.

If none of #required, #empty_value, #empty_option, and #default_value are set, then no empty option is added to the select control. This leaves the control in a slightly illogical state, since all user agents automatically preselect the first available option. There's no way for the user to select nothing, and the user is also not forced to make an active decision.

https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#empty_value

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