Question

I need some help to fix an error on this code:

var image = new Array ();
image[0] = "http://placehold.it/20";
image[1] = "http://placehold.it/30";
image[2] = "http://placehold.it/40";
image[3] = "http://placehold.it/50";
var size = image.length
var x = Math.floor(size*Math.random())

$('#random').attr('src',image[x]);

The error I get when I run the HTA(Hyper Text Application) is: Line: 46 Error: The value of the property '$' is null or undefined, not a Function object. Line 46 is this line:

$('#random').attr('src',image[x]);

The link to the question that I found this code is here

EDIT

Here is the code for my whole program here

Can anyone help me please?

Était-ce utile?

La solution 2

Include jquery at the top of your page and in head tab:

<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>

Try it again:

$(function(){

    var image = new Array ();
    image[0] = "http://placehold.it/20";
    image[1] = "http://placehold.it/30";
    image[2] = "http://placehold.it/40";
    image[3] = "http://placehold.it/50";
    var size = image.length
    var x = Math.floor(size*Math.random())

    $('#random').attr('src',image[x]);

})

Autres conseils

Add Jquery library in your file <head> block

    <script src="//code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
   <script>
    $(window).load(function(){
    var image = new Array ();
    image[0] = "http://placehold.it/20";
    image[1] = "http://placehold.it/30";
    image[2] = "http://placehold.it/40";
    image[3] = "http://placehold.it/50";
    var size = image.length
    var x = Math.floor(size*Math.random())

    $('#random').attr('src',image[x]);
    });
   </script>

DEMO

try this one.. hope it's help

 <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(window).load(function(){
    var image = new Array ();
    image[0] = "http://placehold.it/20";
    image[1] = "http://placehold.it/30";
    image[2] = "http://placehold.it/40";
    image[3] = "http://placehold.it/50";
    var size = image.length
    var x = Math.floor(size*Math.random())

    $('#random').attr('src',image[x]);
    });
   </script>

There are a couple of errors in your code, Here's a working example...

Make sure jQuery is included on your page

$(document).ready({

   // jQuery has now loaded on the page

   var image = [];
   image[0] = "http://placehold.it/20";
   image[1] = "http://placehold.it/30";
   image[2] = "http://placehold.it/40";
   image[3] = "http://placehold.it/50";

   var src = image[Math.floor(Math.random() * image.length)];

   $('#random').attr('src',src);

});

link to example

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top