I have created a view for to search my channels. But when user searches for a specific channel, and when he clicks any other option or on any other event,he redirects to other page but text in search box is not cleared,what i want is, the text in search box should get cleared once he clicks on other option.How can i do this??

2 Answers 2


You can reset the value of search box with Javascript when page get ready. Create a simple custom module and create a js file in that module. Something like:

(function($, Drupal, window, document, undefined) {
})(jQuery, Drupal, this, this.document);

Attach this file to the pages those have that search form.

function custom_form_alter(&$form, &$form_state, $form_id){
  if ($form_id == 'SERCH_FORM_ID') {
    $form['#attached']['js'] = array(
      drupal_get_path('module', 'custom') . '/custom.js' => array(
        'type' => 'file',

Update: updated code as per @Andy comment, adding js with '#attached' property

  • You should use #attached in this situation to add the JS to the form.
    – Andy
    Aug 1, 2014 at 10:22
  • Also that JS won't necessarily play nicely with other modules (for example if you did anything AJAXy, the search query would suddenly disappear). You should write attach handlers such that they can be called multiple times (and you can't know when) without causing problems. Typically .once() is used for this.
    – Andy
    Aug 1, 2014 at 10:46
  • How to add JS to the form? Can you please edit the code?
    – 12345
    Aug 4, 2014 at 5:11
  • @Tejaswini: I updated the code. You could do it yourself. Have a look at #attached form property. api.drupal.org/api/drupal/… Aug 4, 2014 at 6:21
  • That how can i know. That is your views specific. Here SEARCH-FORM-INPUT is the jquery selector for your form's input field. And SEARCH_FORM_ID is your views exposed from id. Views exposed form id are in form of views-exposed-form-VIEWS-MACHINE-NAME-VIEWS-DISPLAY-NAME, Normally views machine name and views display name have underscore(_) in their name, remove those underscore(_) with hyphen(-). Or use dpm($form_id) on first line in hook_form_alter() it will print the form_id of your exposed form. Aug 4, 2014 at 7:55

I think you can do it in 2 ways:

  1. You can use javascript to clear the value of textfield after page load or on click.

  2. You need to rewrite field or rebuild form when option changes.

It`s all that i can say without some code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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