79

I have the jQuery loaded fine, I've quadruple-checked, though I'm still getting this error in FireBug "$ is not a function" and my code doesn't work.

Here's my code:

<script type="text/javascript">
  $("ol li:nth-child(1)").addClass('olli1');
  $("ol li:nth-child(2)").addClass("olli2");
  $("ol li:nth-child(3)").addClass("olli3");
  $("ol li:nth-child(4)").addClass("olli4");
  $("ol li:nth-child(5)").addClass("olli5");
  $("ol li:nth-child(6)").addClass("olli6");
  $("ol li:nth-child(7)").addClass("olli7");
  $("ol li:nth-child(8)").addClass("olli8");
  $("ol li:nth-child(9)").addClass("olli9");
  $("ol li:nth-child(10)").addClass("olli10");
  $("ol li:nth-child(11)").addClass("olli11");
  $("ol li:nth-child(12)").addClass("olli12");
  $("ol li:nth-child(13)").addClass("olli13");
  $("ol li:nth-child(14)").addClass("olli14");
  $("ol li:nth-child(15)").addClass("olli15");
  $("ol li:nth-child(16)").addClass("olli16");
  $("ol li:nth-child(17)").addClass("olli17");
  $("ol li:nth-child(18)").addClass("olli18");
  $("ol li:nth-child(19)").addClass("olli19");
  $("ol li:nth-child(20)").addClass("olli20"); 
</script>
9
  • jQuery is loaded in the header and this script in the footer, so this is not the issue Commented Oct 14, 2010 at 8:55
  • HTML please? Maybe a demo page? Commented Oct 14, 2010 at 8:57
  • 4
    OffTopic: Wow! your going to have 20 different classes for a simple list? shouldn't you approach this in other way? Commented Oct 14, 2010 at 8:58
  • 3
    Haven't called noConflict anywhere, have you? Commented Oct 14, 2010 at 8:58
  • 1
    Please post the smallest possible example in jsbin/jsfiddle where the error occurs, so we get even a little chance to help you. Just that short portion of code plus "... so this is not the issue" is the same as asking my crystal ball what the problem is... Commented Oct 14, 2010 at 9:00

8 Answers 8

195

In Wordpress jQuery.noConflict() is called on the jQuery file it includes (scroll to the bottom of the file it's including for jQuery to see this), which means $ doesn't work, but jQuery does, so your code should look like this:

<script type="text/javascript">
jQuery(function($) { // DOM is ready, and $ alias is now in scope
  for(let i = 1; i <= 20; i++) {  // loop [1-20]
    $(`ol li:nth-child(${i})`).addClass(`olli${i}`);
  }       
});
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

I've replaced my code with this one, but it still doesn't work. This is the page softsailor.com/how-to/…
@Alex - Taking a look, noConflict is definitely your issue with $...see it in your jQuery include at the end: softsailor.com/wp-includes/js/jquery/jquery.js?ver=1.4.2, seeing what else is wrong now.
@Alex - You don't have the code in the answer, your code has var i=i for some reason, it should be var i=0, replace that and it'll work.
by making $ a function i was able to fix my code var $ = jQuery;
@sairfan then you'll get conflicts instead
51

It's really hard to tell, but one of the 9001 ads on the page may be clobbering the $ object.

jQuery provides the global jQuery object (which is present on your page). You can do the following to "get" $ back:

jQuery(document).ready(function ($) {
    // Your code here
});

If you think you're having jQuery problems, please use the debug (non-production) versions of the library.

Also, it's probably not best to be editing a live site like that ...

1 Comment

The shorthand for DOM ready in jQuery is: jQuery(function($) { or if you will: jQuery($ => {. While at it, I would also support OP with a cleaner way to write that repetitive code.
7

In my case I was using jquery on my typescript file:

import * as $ from "jquery";

But that line gives me back an Object: $ and it does not allow to use it as a function (I can not use $('my-selector')). I managed to solve my problem with the lines below. I hope they could help you guys:

import * as JQuery from "jquery";
const $ = JQuery.default;

Comments

6

There are quite lots of answer based on situation.

1) Try to replace '$' with "jQuery"

2) Check that code you are executed are always below the main jquery script.

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){

});
</script>

3) Pass $ into the function and add "jQuery" as a main function like below.

<script type="text/javascript">
jQuery(document).ready(function($){

});
</script>

1 Comment

Your first example jQuery(document).ready(function(){ makes no sense. Always pass the well known $ as the first argument in order to alias jQuery and secure it in the scope of the enclosing function. To avoid confusion you can get rid of that first example entirely.
5
<script type="text/javascript">
    $("ol li:nth-child(1)").addClass('olli1');
    $("ol li:nth-child(2)").addClass("olli2");
    $("ol li:nth-child(3)").addClass("olli3");
    $("ol li:nth-child(4)").addClass("olli4");
    $("ol li:nth-child(5)").addClass("olli5");
    $("ol li:nth-child(6)").addClass("olli6");
    $("ol li:nth-child(7)").addClass("olli7");
    $("ol li:nth-child(8)").addClass("olli8");
    $("ol li:nth-child(9)").addClass("olli9");
    $("ol li:nth-child(10)").addClass("olli10");
    $("ol li:nth-child(11)").addClass("olli11");
    $("ol li:nth-child(12)").addClass("olli12");
    $("ol li:nth-child(13)").addClass("olli13");
    $("ol li:nth-child(14)").addClass("olli14");
    $("ol li:nth-child(15)").addClass("olli15");
    $("ol li:nth-child(16)").addClass("olli16");
    $("ol li:nth-child(17)").addClass("olli17");
    $("ol li:nth-child(18)").addClass("olli18");
    $("ol li:nth-child(19)").addClass("olli19");
    $("ol li:nth-child(20)").addClass("olli20"); 
</script>

change this to

    <script type="text/javascript">
        jQuery(document).ready(function ($) {
        $("ol li:nth-child(1)").addClass('olli1');
        $("ol li:nth-child(2)").addClass("olli2");
        $("ol li:nth-child(3)").addClass("olli3");
        $("ol li:nth-child(4)").addClass("olli4");
        $("ol li:nth-child(5)").addClass("olli5");
        $("ol li:nth-child(6)").addClass("olli6");
        $("ol li:nth-child(7)").addClass("olli7");
        $("ol li:nth-child(8)").addClass("olli8");
        $("ol li:nth-child(9)").addClass("olli9");
        $("ol li:nth-child(10)").addClass("olli10");
        $("ol li:nth-child(11)").addClass("olli11");
        $("ol li:nth-child(12)").addClass("olli12");
        $("ol li:nth-child(13)").addClass("olli13");
        $("ol li:nth-child(14)").addClass("olli14");
        $("ol li:nth-child(15)").addClass("olli15");
        $("ol li:nth-child(16)").addClass("olli16");
        $("ol li:nth-child(17)").addClass("olli17");
        $("ol li:nth-child(18)").addClass("olli18");
        $("ol li:nth-child(19)").addClass("olli19");
        $("ol li:nth-child(20)").addClass("olli20"); 
     });
    </script>

1 Comment

Besides the suggestion on using jQuery as the global instance reference name variable, there are cleaner ways to iterate from [1-20]
4

As RPM1984 refers to, this is mostly likely caused by the fact that your script is loading before jQuery is loaded.

1 Comment

jQuery is loaded in the header and this script in the footer, so this is not the issue.
2

That error kicks in when you have forgot to include the jQuery library in your page or there is conflict between libraries - for example you be using any other javascript library on your page.

Take a look at this for more info:

Comments

2

When jQuery is not present you get $ is undefined and not your message.

Did you check if you don't have a variable called $ somewhere before your code?
Inspect the value of $ in firebug to see what it is.

Slightly out of the question, but I can't resist to write a shorter code to your class assignment:

    var i = 1;
    $("ol li").each(function(){
        $(this).addClass('olli' + i++);
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.