I was needing to add a default value to a text field that disappears when you click on the field.

I did found this question :

How to add default text that clears on focus to a form in Drupal?

However, my knowledge is pretty limited as far as creating hooks so I'm a little lost as to the solution being drawup up here.

Could someone please point me in the right direct?


  • Sorry but we can't write code for you. And we can't write a complete documentation. If you need basics, see how to implement a hook? or Examples for Developers. – Mołot Mar 27 '14 at 10:03
  • Thanks Molot, I should have mentioned that I did look into implementing a hook - but I'm unsure how this fits into achieving my desired effect. ie. How do I then link in the JS file. Cheers (edit: Clive's answers solved it) – dangerranger Mar 27 '14 at 10:10

Since this is 2014 you're probably looking for the placeholder attribute, and not an old-school javascript solution.

Broad strokes: Implement hook_form_alter() and add the attribute there:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'foo') {
    $form['bar']['#attributes']['placeholder'] = t('Baz');
  • 1
    Legend! Cheers Clive, this is exactly the solution I was looking for. – dangerranger Mar 27 '14 at 10:12

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.