JavaScript Generating random image url in javascript generate random image,generate random image python,generate random image

I am generating random numbers and appending with the previous variable and trying to get the value of images. but also the random number only generated. How can I do this ?

Here's my code

var img1 = 'http://i.imgur.com/8olCb1Qb.jpg';
var img2= 'http://i.imgur.com/usJWgL7b.jpg';
var img3 = 'http://i.imgur.com/kxsLXb8b.jpg';
var img4 = 'http://i.imgur.com/XQbcjvUb.jpg';
var img5 = 'http://i.imgur.com/j3CVSSMb.jpg';
var img6 = 'http://i.imgur.com/BQNvBVib.jpg';
var img7 = 'http://i.imgur.com/DZq0ORlb.jpg';
var img8 = 'http://i.imgur.com/t73Tvlqb.jpg';
var img9 = 'http://i.imgur.com/Y8iFltdb.jpg';
var img10 = 'http://i.imgur.com/u3sBUMjb.jpg';
var no = Math.floor(Math.random() * 6) + 1
var img = 'img'+no;
console.log(img)

I prefer only javascript.

Answer:1

I think you want to select random images

var images = [
    'http://i.imgur.com/8olCb1Qb.jpg',
    'http://i.imgur.com/usJWgL7b.jpg',
    'http://i.imgur.com/kxsLXb8b.jpg',
    'http://i.imgur.com/XQbcjvUb.jpg',
    'http://i.imgur.com/j3CVSSMb.jpg',
    'http://i.imgur.com/BQNvBVib.jpg',
    'http://i.imgur.com/DZq0ORlb.jpg',
    'http://i.imgur.com/t73Tvlqb.jpg',
    'http://i.imgur.com/Y8iFltdb.jpg',
    'http://i.imgur.com/u3sBUMjb.jpg'
];
var random = images[Math.floor(Math.random() * images.length)];
Answer:2

well. arrays are fun. you should use them.

var images = ["8olCb1Qb", "usJWgL7b", "kxsLXb8b", "XQbcjvUb", "j3CVSSMb", "BQNvBVib", "DZq0ORlb", "t73Tvlqb", "Y8iFltdb", "u3sBUMjb"];
var randomPick = images[Math.random() * images.length | 0];
var url = "http://i.imgur.com/" + randomPick + ".jpg";
document.body.appendChild(new Image()).src = url;
Answer:3

As suggested by others, dynamic variable names are kind of ugly. You should use an array.

var imgs = ['a', 'b', 'c'];
var randomImg = imgs[Math.floor(Math.random() * imgs.length);

If you really want to use dynamic names, you should attach them to an object, not just free standing variables.

var imgs = {img1: 'a', img2: 'b', img3: 'c'};
var no = Math.floor(Math.random() * 3) + 1:
var img = imgs['img' + no];
Answer:4

Put your images in a div named div divImages.

var img=$('#divImages').children().eq(no);

And if you don't want using jquery the javascript is:

var img = document.getElementById('#divImages').children[no];
Answer:5

I continuously run into this issue when designing sites that have scroll-based events (animation, parallax, etc etc etc). Is there any reliable to test if a browser halts repainting on scroll, so I ...

I continuously run into this issue when designing sites that have scroll-based events (animation, parallax, etc etc etc). Is there any reliable to test if a browser halts repainting on scroll, so I ...

<ul> <a href="#Project"> <li onclick="project()">Projects</li> </a> </ul> a tab appears and you can click on it, which runs a javascript function ...

<ul> <a href="#Project"> <li onclick="project()">Projects</li> </a> </ul> a tab appears and you can click on it, which runs a javascript function ...

I wish to animate a div to make it appear and slide down with jQuery. I have got my script to work where you hover over an image and another div slides in, should the user leave the mouse hover, the ...

I wish to animate a div to make it appear and slide down with jQuery. I have got my script to work where you hover over an image and another div slides in, should the user leave the mouse hover, the ...

I have been trying to grab json data from an API and display it using the map() function. Unfortunately the API returns data in the format: { "type": ..., "value": ... }. The second object value ...

I have been trying to grab json data from an API and display it using the map() function. Unfortunately the API returns data in the format: { "type": ..., "value": ... }. The second object value ...

  1. displaying json data in html
  2. displaying json data in react js
  3. displaying json data in angular
  4. displaying json data in html table
  5. displaying json data in php
  6. displaying json data in html using javascript
  7. displaying json data in python
  8. displaying json data in angular component
  9. displaying json data in chart js
  10. displaying json data in angular 6
  11. displaying json data
  12. displaying json data in javascript
  13. displaying json data in html using jquery
  14. displaying json data in datatable
  15. displaying json data in table
  16. displaying json data in android
  17. displaying json data in angularjs
  18. displaying json data in ionic
  19. displaying json data in jsp
  20. jquery displaying json data