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
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:
//custom.js
(function($, Drupal, window, document, undefined) {
$(document).ready(function(){
$('SEARCH-FROM-INPUT').val('');
});
})(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
-
-
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.– AndyCommented Aug 1, 2014 at 10:46 -
-
@Tejaswini: I updated the code. You could do it yourself. Have a look at
#attached
form property. api.drupal.org/api/drupal/… Commented 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. AndSEARCH_FORM_ID
is your views exposed from id. Views exposed form id are in form ofviews-exposed-form-VIEWS-MACHINE-NAME-VIEWS-DISPLAY-NAME
, Normally views machine name and views display name haveunderscore(_)
in their name, remove thoseunderscore(_)
withhyphen(-)
. Or usedpm($form_id)
on first line inhook_form_alter()
it will print theform_id
of your exposed form. Commented Aug 4, 2014 at 7:55
I think you can do it in 2 ways:
You can use javascript to clear the value of textfield after page load or on click.
You need to rewrite field or rebuild form when option changes.
It`s all that i can say without some code.