28

OK, I know this has been asked before but none of the answers seems to apply to my case. I'm trying to get a very tiny piece of jQuery running (I'm just getting started on it).

jQuery(document).ready(function(){
    jQuery('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
})();

I get the error TypeError: jQuery(...).ready(...) is not a function in FF or Uncaught TypeError: object is not a function in Chrome.

  • Solution 1 was to replace $ with jQuery but I obviously already did that as shown above
  • I'm not in Wordpress either
  • I'm using only jQuery and the above mini script, no other JS
  • jQuery itself seems to load fine enter image description here

What am I missing here?

7
  • 1
    What happens if you type $ or jQuery into console in browser? Commented Feb 13, 2014 at 13:19
  • 2
    Just to be sure, are you loading jQuery before your jQuery(...).ready(...) call? Commented Feb 13, 2014 at 13:19
  • what is the order of jquery file including? Commented Feb 13, 2014 at 13:20
  • 4
    I think there is a typo })(); Commented Feb 13, 2014 at 13:20
  • 2
    Replace })(); with });, This is incorrect {()) Commented Feb 13, 2014 at 13:26

5 Answers 5

48

try to remove this (); at the end of doc ready:

jQuery(document).ready(function(){
  jQuery('.comtrig').on('click',function(){
    $(this).next().animate({'display':'inline'},1000);
  });
}); //<----remove the (); from here

(); is normally used to have a Immediately-Invoked Function Expression (IIFE) which has some kind of syntax like this:

(function(){
   // your stuff here
})(); //<----this invokes the function immediately.

Your errors:

in firefox = TypeError: jQuery(...).ready(...) is not a function

in chrome = Uncaught TypeError: object is not a function

because:

Your document ready handler is not a Self-executing anonymous function.

Sign up to request clarification or add additional context in comments.

5 Comments

@Jai: I see! () is included for a SIAF and the $(document).ready syntax is NOT a SIAF. Thanks, that's a helpful disambiguation you wrote.
@RubenGeert When you call .ready(function() { ... }), you are calling ready() and passing an anonymous function as a parameter. When you call .ready(function() {...})(), you are doing the same as the above, but then trying to execute the result of ready() as a function. .ready() returns a jquery object, which is not a function.
@RubenGeert see Self-executing anonymous function or Immediately-Invoked Function Expression used to have some special syntax to do this, while jQuery is desined and developed that way, so there is no need to do this in your case, although you can make a closure to save your $ alias to jQuery for use in your closure if you have used multiple libraries which uses the $.
@downvoter why downvote after 12 upvotes. i guess it really needs a comment for that right.
@downvoter well i get a second downvote i don't know why?
8

There are two issues in the code.

1 - The brackets at the end of the code.

2 - $(this) should have been jQuery(this) or $ inside function.

jQuery(document).ready(function($){
    $('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
});

2 Comments

But you completely failed to explain WHAT you changed, and WHY you changed it. Maybe if you'd explained, people would have taken a moment to understand...
I completely agree with @NiettheDarkAbsol, Yours is first correct answer, but without explanation
3

Remove the extra brackets () at the end. Keep the code as below.

jQuery(document).ready(function(){
    jQuery('.comtrig').on('click',function(){
        $(this).next().animate({'display':'inline'},1000);
    });
}); // <== remove () from here

Comments

1

Passing jquery object this way works for me.

$(document).ready(function () {
    console.log("jquery");
}(jQuery));

Comments

0

These Errors:

    TypeError: jQuery(...).ready(...) is not a function
    or
    Uncaught TypeError: object is not a function

are also happens if you implement Jquery library after your code, it should be before, ORDER MATTER here.

 <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.