views:

115

answers:

7

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>
A: 

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

mkoistinen
jQuery is loaded in the header and this script in the footer, so this is not the issue.
Alex
A: 

give us an adress

kleampa
learn how to use StackOverflow please, this should be a comment, not an answer !
balexandre
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
@balexandre - You can't comment with < 50 rep.
Nick Craver
A: 

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
The jQuery is included automatically by Wordpress. I tried that and it still doesn't work.
Alex
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
it's ok, I deserved it ;) cheers
Marko
A: 

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
A: 

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
A: 

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
+5  A: 

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
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
Also thanks for optimizing it :)
Alex
@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
@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
It works. Thanks a lot !
Alex
@Alex - welcome!
Nick Craver