Emerson Maningo
Emerson Maningo

Reputation: 2279

Uncaught TypeError: object is not a function when changing jQuery loading method

This is related here: Running jQuery inside $(window).load() function but not inside $(document).ready function

Before I am using:

jQuery( document ).ready( function( $ ) {

to load my jQuery UI position code but I decided to try to load using:

jQuery(window).load(function($) {

Now I get an error:

Uncaught TypeError: object is not a function

This is my code before the change:

<script type='text/javascript'>
jQuery( document ).ready( function( $ ) {
var element_selector='.test';
if ( $(element_selector).length !== 0) {

var divname515e62e8355b0 = '#test_selector';
$(divname515e62e8355b0).children().wrapAll('<div class="mydoc cf">');

//jQuery UI Position code here

}

 });
</script>

This is my code after the change:

<script type='text/javascript'>
jQuery(window).load(function($) {
var element_selector='.test';
if ( $(element_selector).length !== 0) {

var divname515e62e8355b0 = '#test_selector';
$(divname515e62e8355b0).children().wrapAll('<div class="mydoc cf">');

//jQuery UI Position code here

}

 });
</script>

But I get an error:

Uncaught TypeError: object is not a function

This is the problem line:

$(divname515e62e8355b0).children().wrapAll('<div class="mydoc cf">');

I've checked for commas, semicolons and there seems fine. What could be the problem?

Thanks for any tips.

Upvotes: 2

Views: 6333

Answers (1)

Ram
Ram

Reputation: 144739

That's because in your second code, $ is an event object, .load() doesn't behave like .ready() method, If you want to avoid conflict use a self-invoking function:

(function($) {
    $(window).load(function(event) {
        // ...
    });
})(jQuery);

Upvotes: 5

Related Questions