Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: $(...).ready is not a function

Tags:

jquery

Hi I Know this has been asked before but no answer on here seems to help me.

I have this block of JS:

$(document).ready(function() {
    $('.play-icon-hover').hover(function() {
         $('.cms-model-banner-overlay').addClass('.cms-model-banner-overlay-active');
        }, function() {
         $('.cms-model-banner-overlay').removeClass('.cms-model-banner-overlay-active');
    });
});

And I seem to be getting this error but I have no idea why?

Uncaught TypeError: $(...).ready is not a function

Thanks

like image 498
Nick M Avatar asked Oct 10 '15 16:10

Nick M


People also ask

How do I fix uncaught TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

Is not a function $( document .ready function ()?

ready is not a function" error occurs for multiple reasons: Placing second set of parenthesis after the call to the ready() method. Loading the jQuery library after running your JavaScript code. Forgetting to load the jQuery library.

What is $( document .ready function ()?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is the difference between $( Windows .load & $( document .ready function in jQuery?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.


1 Answers

You are using Prototype.js as well as jQuery.js. If you wanna use jQuery, it is better to encapsulate your code inside an IIFE like this:

(function ($) {
  // jQuery code using $
})(jQuery);

So the solution for your issue is either you change $ to jQuery:

jQuery(document).ready(function() {
  jQuery('.play-icon-hover').hover(function() {
    jQuery('.cms-model-banner-overlay').addClass('.cms-model-banner-overlay-active');
  }, function() {
    jQuery('.cms-model-banner-overlay').removeClass('.cms-model-banner-overlay-active');
  });
});

Or, use a IIFE:

(function ($) {
  $(document).ready(function() {
    $('.play-icon-hover').hover(function() {
      $('.cms-model-banner-overlay').addClass('.cms-model-banner-overlay-active');
    }, function() {
      $('.cms-model-banner-overlay').removeClass('.cms-model-banner-overlay-active');
    });
  });
})(jQuery);
like image 53
Praveen Kumar Purushothaman Avatar answered Oct 19 '22 03:10

Praveen Kumar Purushothaman