jQuery included with WordPress is in compatibility mode. To avoid conflicts with other libraries we can not use the ‘$’ shortcut for ‘jQuery’.

To use the ‘$’ sign we use:

jQuery(document).ready(function($) {
   $('#myid').css({'background': 'black', 'color':'white'});
});

This works. But my question is how to do the same with window load. I have been facing this problem since last few projects. So, thought better to make the concept clear.

jQuery(window).load(function($) {
   $('#myid').css({'background': 'black', 'color':'white'});
});

With this I get an error that says : “$ is not a function”. So, I am unable to use $ inside of the window.load code block.

So, can anyone help how I can use the $ shortcut inside window.load?

2 Answers
2

Using a self invoking anonymous function that passes the jQuery object will do the trick:

(function($){ 
   $(window).load(function(){
     $('#myid').css({'background': 'black', 'color':'white'});
   });
})(jQuery); //Passing the jQuery object as a first argument

Leave a Reply

Your email address will not be published. Required fields are marked *