Skip to: Site menu | Main content

WTF: How do I get Drupal to put placeholder text inside my textfield?

What's that feature: the #attributes key.

Perhaps you have written your own javascript code to determine that an empty textfield does not have focus, and in such a situation place some placeholder text in the field. If you are using the FAPI there is an easier way. You can do this:

$form['my_textfield'] = array(
  '#type' => 'textfield',
  '#attributes' => array(
    'placeholder' => t('My placeholder text'),

Take note that this is telling Drupal to use the placeholder="My placeholder text" attribute for the textfield. The placeholder attribute is an HTML5 attribute. Keep this in mind when considering what browsers will be accessing your site and whether it's important for all visitors to see the placeholder text.

You can tell Drupal to use any (valid) HTML attributes with this technique.

This really-easy-to-use feature was one that I had forgotten about. So, I figured others may have forgotten about it as well. I hope you enjoy it!

See also: Label Trash module

Great tip Marc!

This is a very useful tip for Drupal programmers creating forms.

If you're not a programmer you can still create textfields with placeholders on your site by using the Label Trash module.

Label Trash will turn textfield labels on any existing page on your site into inline placeholders. It doesn't matter whether the textfield were created by core or by a contrib module.

Moreover, through its UI, Label Trash then let's you apply the "Float Label" pattern as well. Try it out yourself here:


Credits: XWeb | Chris Herberte | Andreas Viklund