0

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;
});

4 Answers 4

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

1
  • 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, 2012 at 18:17
2

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');
0

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.

-1

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 )))

1
  • 2
    This doesn't seem to answer the question in any way...
    – Clive
    Oct 25, 2012 at 18:49

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.