2

I am using advagg module to improve my website performance and I also using date picker jquery UI function for date functionality.

I have datePicker intialization $( "#datepicker" ).datepicker(); . It works perfectly, I can Even see the calender but when I am selecting the date, I don't see any alert.

for Some reason On datepicker function onSelect is not working when I have advagg enabled.

 jQuery('#datepicker').datepicker({
    onSelect: function(dateText, inst) {
        alert(dateText);
        glImgExpiry = dateText;
      }
});

If I disabled advagg, It works without any problem and I can see alert(dateText). This is happening when I am configuraing advagg to my dev/test , In my local I am not able to configure correctly because I only see per file setting for jscompression and other settings in my dev/test.So sadly, I am not able to reproduce same issue in my local and it is breaking in my test/dev environment.

I know the information, I am giving is not sufficient to get the issue/ problem I am describing. I am just hoping some one might have faced same kind of issue and can help me resolve this problem.

EDIT

As suggested I have tried putting the datepicker onSelect function inside drupal.behavior

(function($) {
    //add drupal 7 code
    Drupal.behaviors.myfunction = {
        attach: function(context, settings) {
//end drupal calls

            jQuery(document).ready(function() {
                jQuery('#gl_img_datepicker').datepicker({
                    onSelect: function(dateText, inst) {
                        alert(dateText);
                     }
                });
//some jquery goodness here...
            });

        }};
})
        (jQuery);

But I still dont see the alert(dateText); in the page.

Thanks your help is really appreciated.

  • 1
    how are you adding you js? Is your js contained within a drupal.behavior? – 2pha Oct 15 '14 at 5:47
  • using drupal_add_js($path_to_module . '/js/myjsFile.js'); – Hitesh Oct 15 '14 at 5:48
  • 1
    in what function are you calling drupal_add_js? Is your js in a drupal.behavior? Are there any js errors reported on the page? – 2pha Oct 15 '14 at 5:52
  • I have custom module and in hook function and in mymodule.inc file I am including the drupal_add_js($path_to_module . '/js/myjsFile.js'); at the top for that file – Hitesh Oct 15 '14 at 6:08
  • 1
    If you are not using behaviors, you may expect your code to conflict with other Drupal modules. Wrap it with behavior and see again. – Mołot Oct 15 '14 at 7:13
0

You do not need to add document.ready to a behavior.
Read about drupal behaviors HERE or at a good blog post HERE.
Basically I think Drupal adds a document.ready which calls all the behaviors. The behaviors are also called after an ajax call is returned (sending the new content returned by the ajax to the behaviors via the context variable). Which is why we use the context variable in the jquery selector.
Your javascript should probably look like this.

(function ($) {
  Drupal.behaviors.myfunction = {
    attach: function (context, settings) {
      $('#gl_img_datepicker', context).datepicker({
        onSelect: function(dateText, inst) {
          alert(dateText);
        }
      });
    }
  };
})(jQuery);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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