1

I have a drop down list. On the change event of the drop down list I have written some javascript code. When I select an option from the drop down list a site opens in the new window.

So for this I wrote some javascript code on the change event of the drop down list.

But the problem is that when I enable the optimize js files then my drop down change event does not work and when I disable the optimize js files then the change event of the drop down works.

So what should I do for this to work in both condition?

UPDATED:

Drupal.behaviors.change = function (context) {


    $(".test").change(function(){
        var page = $(".test").val();
        window.open(''+page+'', '_blank');
    }) ;

};
8
  • Is it a Jquery script? Commented Jun 19, 2012 at 6:43
  • yes, it is a jquery script.
    – Ahmad
    Commented Jun 19, 2012 at 6:45
  • Can you look into the error console Ctrl + Shift + J and post the errors ? If its an error like $ is not defined then its because Jquery Library is loaded after your scripts are loaded as said by ninjascorner
    – GoodSp33d
    Commented Jun 19, 2012 at 8:02
  • It might be helpful to post your JavaScript.
    – Chapabu
    Commented Jun 19, 2012 at 8:32
  • I have posted my javascript.
    – Ahmad
    Commented Jun 19, 2012 at 10:51

2 Answers 2

1

The reason maybe is your custom script loads first before the Jquery script.

Try this:

(function($) {
  $(function() { 
    //your script here
  });
})(jQuery);

or

Drupal.behaviors.somethingHere = function (context) {
  //your script here
}
4
  • 1
    It may also be cache problem. Try to flush the cache and see what happens?
    – Mika A.
    Commented Jun 19, 2012 at 6:59
  • I tried both of your methods but it also did not work. See my question again, I have updated it with my js code.
    – Ahmad
    Commented Jun 19, 2012 at 10:50
  • How/where did you add the script? Commented Jun 20, 2012 at 9:09
  • direct in the module file using drupal_add_js("js_code", "inline", "footer", FALSE, FALSE);
    – Ahmad
    Commented Jun 20, 2012 at 9:30
0

This is a shot in the dark, but when you concatenate Javascript files, the problem could actually be from another file and not the one that isn't working. I would try using

;Drupal.behaviors.change = function (context) {
    $(".test").change(function(){
        var page = $(".test").val();
        window.open(''+page+'', '_blank');
    }) ;
};

Note the semicolon at the beginning. If the previous file is missing a semi-colon, it may work on its own, but result in a syntax error when you concatenate. Out of habit, everything I declare at the top-level gets these.

1
  • did not work :(
    – Ahmad
    Commented Jun 20, 2012 at 4:57

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.