-1

I've got the error 'Uncaught TypeError: jQuery(...).appear is not a function drupal' when trying to use the script below:

content of script.js:

Drupal.behaviors.basic = {
   attach: function (context, settings) {
     (function ($) {
          jQuery('.hero').each(function() {
            jQuery(this).appear(function() {
              var $endNum = parseInt(jQuery(this).find('.number').text());
              jQuery(this).find('.number').countTo({
                from: 0,
                to: $endNum,
                speed: 4000,
                refreshInterval: 60,
                formatter: function (value, options) {
                  value = value.toFixed(options.decimals);
                  value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
                  return value;
                }
              });
            },{accX: 0, accY: 0});
          });
})(jQuery);
}
};

the html:

<div class="hero">
  <div class="number">1,234</div>
</div
0

Here is how I would debug this issue.

  1. Check if your jQuery object is attached to the global window object. You can do this by typing window.jQuery on your browser console. If this returns 'Undefined' then the issue is with the reference to the jquery library.

  2. Make sure that the jQuery library is loading properly. Inspect your Network tab in your browser to find this out.

  3. Make sure your js has - core/jquery as a dependency i.e.

      global-js:
       js:
        js/myjs.js: {}
       dependencies:
         - core/jquery
    

Also, you do not need to use jQuery after you pass 'jQuery' object from the global scope into your IIFE. You should just use '$'. Look at this article for more info.

https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

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.