Customising Search Box in Search Results Adding OnBlur and OnFocus Attributes I am trying to add custom #attributes to the search_form in the search results page. I get the $form_id from devel which is $form_id = "search_form".

Here is my hook_form_alter() implementation:

function nm_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    if($form_id == 'search_form' ){
    $form['search-form']['#attributes']['onblur'] = "if (this.value == '') {this.value = '".$form['search-form']['#value']."';}";
    $form['search-form']['#attributes']['onfocus'] = "if (this.value == '".$form['search-form']['#value']."') {this.value = '';}";
  }
} 


I checked if the condition is right inside the if statement with the following code, but nothing happened.

dpm($form['search-form'])


So, I am suspecting that the if condition is right.
If you want to add attributes to the form itself you need to do it on the $form array:

$form['#attributes']['onblur'] = "if (this.value == '') {this.value = '".$form['search-form']['#value']."';}";


I'm just guessing but I think you're expecting there to be a search-form element because that's the CSS ID of the form? That's not quite how it works with the FAPI, the elements are in a hierarchy that will eventually be rendered down to HTML. Until that rendering happens the elements have no concept of an ID/class etc., so you won't find them in the array under the ID/class.