-5

Im trying to animate my website but i keep getting the error "$ not defined" here's my code:

$(document).ready(function() {

 spectrum();

  function spectrum(){
    var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() *      256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
    $('#div').animate( { backgroundColor: hue }, 1000);
    spectrum();
 }

});
lehermj
  • 916
  • 4
  • 9
  • 20

3 Answers3

1

It's because you don't have included JQuery. Include JQuery before your script:

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
zmbq
  • 38,013
  • 14
  • 101
  • 171
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
0

Add this line to reference jQuery...

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
EricBellDesigns
  • 955
  • 1
  • 9
  • 33
0

Use a fallback method in case the CDN is not available. This means that you'll have to download the version of jQuery you want to use as well as linking to the CDN --

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='path/to/jquery-1.9.min.js'
 type='text/javascript'%3E%3C/script%3E"));
}
</script>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119