-1

this is the code am using for random images .

 var image = [];

 image[0] = "https://s3.amazonaws.com/tripmeetup/image1.jpg" ;
 image[1] = "https://s3.amazonaws.com/tripmeetup/image2.jpg";
 image[2] = "https://s3.amazonaws.com/tripmeetup/image3.jpg";
 image[3] = "https://s3.amazonaws.com/tripmeetup/image4.JPG";
 var size = image.length

 var x = Math.floor(size*Math.random())

   $("apt_banner").css({"background-image": "url(" +image[x]+ ")"});

in html

but the above code is not working for me . dont know where am going wrong

  • Don't use the `var image = new Array()` syntax, it is deprecated in favour of `var image = []` – jonny Aug 11 '15 at 10:27
  • what is the correct way to use it – Santosh Kumar Aug 11 '15 at 10:29
  • ...I refer you to my previous comment. Use square brackets to declare an array instead. – jonny Aug 11 '15 at 10:29
  • 1
    possible duplicate of [Setting background-image using jQuery CSS property](http://stackoverflow.com/questions/512054/setting-background-image-using-jquery-css-property) – Eloims Aug 11 '15 at 10:30
  • @JonathanBrooks deprecated? Where do you read it? That you said is a shorthand but `new Array()` is not deprecated. Is the object instance. Share with us where do you read this. – Marcos Pérez Gude Aug 11 '15 at 10:30
  • There doesn't appear to be anything wrong with your code, can you post the html? – DickieBoy Aug 11 '15 at 10:30
  • thank you i corrected it . is the code am using in html . am able to load images randomly . but i want to know how to load the same from css background – Santosh Kumar Aug 11 '15 at 10:31
  • @SantoshKumar learn to ask in stackoverflow. You can't put a block of code and no explanation of how do you attempt or what do you want to achieve. – Marcos Pérez Gude Aug 11 '15 at 10:32
  • @MarcosPérezGude The convention is deprecated, not the standard – jonny Aug 11 '15 at 10:36
  • I need documentation to trust in that you say. Here you are a deprecated and obsolete functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features there isn't `new Array()` in nowhere. – Marcos Pérez Gude Aug 11 '15 at 10:37

1 Answers1

1

This should do, myOjbect this should be your selector ( class, id or may be <body> tag)

$('myOjbect').css('background-image', 'url(' +image[x]+ ')');
Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45