Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a Drupal 7 view page where I want to change the behaviour of the submit button by using the jQuery code below. How do I do this in Drupal 7?

$('#reset-button').click(function() {
  $('#views-exposed-form').each(function() {
    this.reset();
  });
  $('#submit-button').click();

  return false;
});
share|improve this question
add comment

4 Answers 4

You can add your code in a JS file say somescript.js and save it in your theme folder. In the theme.info file add the following line

scripts[] = somescript.js

This will add somescript.js to all pages by default. If you want it for a particular page add a conditional check and use drupal_add_js(PATH_TO_JS)
For more info check out Managing JavaScript in Drupal 7

share|improve this answer
1  
WhiteMonk's point about adding the context wrapper around the code is important too. Also a good idea is to use behaviors. –  goron Nov 4 '12 at 18:17
add comment

To use $ you have to introduce

jQuery(function($) {});

at the begining.

jQuery(function($) {
    $(document).ready(function(){
      $('#reset-button').click(function() {
       $('#views-exposed-form').each(function() {
         this.reset();
        });
       $('#submit-button').click();

       return false;
      });
     });
 });

To add this js file to a page you have to use

 drupal_add_js(drupal_get_path('module', 'module_name').'/path/to/js');
share|improve this answer
add comment

If you do not want to write a whole new module to inject custom JavaScript code, then try this module http://drupal.org/project/js_injector.

I have been using this module from long time and it works beautifully.

share|improve this answer
add comment

Good idea! Regarding this I found painless solution. I installed jQuerify from here https://chrome.google.com/webstore/detail/jquerify/gbmifchmngifmadobkcpijhhldeeelkc It injects jQuery 1.8 even in HTTPS pages with one click with no conflict to Prototype )))

share|improve this answer
2  
This doesn't seem to answer the question in any way... –  Clive Oct 25 '12 at 18:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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