74

I have the jQuery loaded fine, I've quadruple-checked, though I'm 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>
Darko
  • 38,310
  • 15
  • 80
  • 107
Alex
  • 4,674
  • 5
  • 38
  • 59

11 Answers11

187

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($) {
    for(var i=0; i <= 20; i++) 
      $("ol li:nth-child(" + i + ")").addClass('olli' + i);
  });
</script>
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1
    I've replaced my code with this one, but it still doesn't work. This is the page http://www.softsailor.com/how-to/46957-how-to-use-greenpois0n-to-jailbreak-iphone-4-3gs-ipad-ipod-touch-ios-4-14-0.html – Alex Oct 14 '10 at 09:13
  • 1
    @Alex - Taking a look, noConflict is definitely your issue with `$`...see it in your jQuery include at the end: http://www.softsailor.com/wp-includes/js/jquery/jquery.js?ver=1.4.2, seeing what else is wrong now. – Nick Craver Oct 14 '10 at 09:18
  • 1
    @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. – Nick Craver Oct 14 '10 at 09:21
  • Wow, I would've never thought of that. – The Onin Aug 29 '15 at 04:38
  • 1
    by making $ a function i was able to fix my code `var $ = jQuery;` – sairfan Dec 19 '18 at 16:28
  • @sairfan then you'll get conflicts instead – OrangeDog Apr 20 '21 at 12:46
49

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 ...

strager
  • 88,763
  • 26
  • 134
  • 176
6
<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>
Roshan Padole
  • 390
  • 4
  • 11
6

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

import * as $ from "jquery";

But this line gives me back an Object $ and it does not allow to use as a function (I can not use $('my-selector')). It solves my problem this lines, I hope it could help anyone else:

import * as JQuery from "jquery";
const $ = JQuery.default;
Luillyfe
  • 6,183
  • 8
  • 36
  • 46
  • 1
    I needed to do this in typescript, const $ = (JQuery as any).default; but your solution worked thanks – patrick Nov 21 '18 at 21:49
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>
Mayank Dudakiya
  • 3,635
  • 35
  • 35
4

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

mkoistinen
  • 7,724
  • 3
  • 41
  • 56
  • 1
    jQuery is loaded in the header and this script in the footer, so this is not the issue. – Alex Oct 14 '10 at 08:54
1

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:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

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++);
    });
Mic
  • 24,812
  • 9
  • 57
  • 70
-2

Use jQuery for $. I tried and work.

-2

I believe using $ alone is now deprecated in the new version of jQuery. Use below syntax instead:

jQuery(function($) {
//magic here
})
Rosalito Udtohan
  • 187
  • 1
  • 11
-4

There are two possible reasons for that error:

  1. your jquery script file referencing is not valid
  2. try to put your jquery code in document.ready, like this:

    $(document).ready(function(){

    ....your code....

    });

cheers

Marko
  • 1,874
  • 1
  • 21
  • 36
  • 1
    The jQuery is included automatically by Wordpress. I tried that and it still doesn't work. – Alex Oct 14 '10 at 09:05
  • 2
    I don't think the use of, or non-use of the document ready construct would make any difference at all here. Also, if '$' isn't yet defined, how can you call $(document.ready(...) ? Sorry, -1. – mkoistinen Oct 14 '10 at 11:48