3

I try to add content on a div through AJAX on Wordpress. I use the JSON API to get posts from a specific category. Here is the code:

<div id="content" style="display:none;">This is my test</div>

<script type="text/javascript">


  $(document).ready(  
    function() {
      setInterval(function() {$.getJSON('/_jquerydownloaddata', 
           function(data) {
            if (data) {

              $("#content").show();
              $("#content").html(data)

            } else {

              $("#content").hide();

            }

           })}, 5000);
    });

    </script>

If I run it local without Wordpress, it runs without a problem. However, now I have this error on the $(document).ready line:

TypeError: $ is not a function

5
  • 3
    Change $ to jQuery and see if that helps. Basically, the error is trying to help you – $ really isn't a function, so you'll need to verify that jQuery has been loaded on the page above the script tag that you've included here. Commented Jan 27, 2015 at 17:31
  • So where did you define $? Commented Jan 27, 2015 at 17:31
  • You need to use jQuery in noconflict mode in Wordpress. Commented Jan 27, 2015 at 17:32
  • 1
    You could pass jQuery like that: jQuery(document).ready(function($){...}); Commented Jan 27, 2015 at 17:40
  • @CharlieS Can you add your comment as an answer to accept it? Commented Feb 9, 2015 at 17:05

1 Answer 1

3

Change $ to jQuery and see if that helps. Basically, the error is trying to help you – $ really isn't a function, so you'll need to verify that jQuery has been loaded on the page above the script tag that you've included here.

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

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.