# Random strip background on homepage from a collection of images.

**URL:** <https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [January 24, 2019, 4:01pm UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118 "2019-01-24T16:01:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alees](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/alees/32/8524_2.png) [@alees](https://forum.wixstudio.com/u/alees)\
**Post date:** [January 24, 2019, 4:01pm UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/1 "2019-01-24T16:01:13Z")

</div>

Hi,

How would I go about setting a strip background image to be random each time the home page loads from a collection of images.

At the moment I’ve created a database with the images in and linked that to the background image of the stripe. But this just sets the first image as the background all the time.

What code do I need to add to have a random image each time?

I’ve tried various searches and things but the code I’ve tried so far doesn’t seem to work. (Bit new to this wix code)

Thanks

Adam

---

<div class="post-metadata">

**Author:** ![mhammouz](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mhammouz/32/4033_2.png) [@mhammouz](https://forum.wixstudio.com/u/mhammouz)\
**Post date:** [January 30, 2019, 11:19am UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/2 "2019-01-30T11:19:31Z")

</div>

Hi **Adam** ,

First, get the images from the dataset, then get its length.  
now you can use Math.floor(Math.random() \* imagesLength), finally you can display random image.  
for more information, here is some links to help you :

- [Random Value from array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array)
- [Good example ( Random Image )](https://www.grampsworkbench.com/Examples/Random-Image)
- and [here](https://www.wix.com/code/home/forum/community-discussion/random-pic-homepage) is previous same question.

* * *

Your code should look something like this:

```javascript
 let results= await wixData.query("ataset").find();
 let images= results.items;
 let count = images.length;
 let randomImageNumber = Math.floor(Math.random() * count);
        $w('#stripColumn').background.src = images[randomImageNumber].image; // get a random image
       
    

```

Hope this helps!  
Best,

Mustafa

---

<div class="post-metadata">

**Author:** ![danvanmoll](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/danvanmoll/32/107322_2.png) [@danvanmoll](https://forum.wixstudio.com/u/danvanmoll)\
**Post date:** [February 27, 2019, 3:02pm UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/3 "2019-02-27T15:02:46Z")

</div>

But how / where would I define the actual location of the images to randomize/display? Ideally a folder with content to be used. I am a little lost when it comes to coding - would highly appreciate a short step-by-step explanation. Thanks!!

---

<div class="post-metadata">

**Author:** ![trikalero](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/trikalero/32/9923_2.png) [@trikalero](https://forum.wixstudio.com/u/trikalero)\
**Post date:** [September 16, 2019, 12:09pm UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/4 "2019-09-16T12:09:44Z")

</div>

await value is wrong when I preview the page with the code. What should I do; Thanks in advance!

---

<div class="post-metadata">

**Author:** ![sinajackie70](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sinajackie70/32/26053_2.png) [@sinajackie70](https://forum.wixstudio.com/u/sinajackie70)\
**Post date:** [January 12, 2021, 3:23am UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/5 "2021-01-12T03:23:48Z")

</div>

Here is the code I hope It would useful for you :  
$w.onReady( **function** () {  
// To make this Code work in all of your site pages,  
// Move these lines into the the ‘Site Code’ panel on the left

```
    $w( "#imageX1" ).hide(); 
    $w( "#imageX2" ).hide(); 

```

**let** num = Math.floor(Math.random()\* 10 )  
num = num + 1 ;

**if** ( num \< 5 ){  
$w( “#imageX1” ).show();  
$w( “#imageX2” ).hide();  
console.log( “state1” )  
}  
**else** {  
$w( “#imageX1” ).hide();  
$w( “#imageX2” ).show();  
console.log( “state2” )  
}  
console.log(num)  
});

---

<div class="post-metadata">

**Author:** ![grantmore](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@grantmore](https://forum.wixstudio.com/u/grantmore)\
**Post date:** [April 7, 2021, 6:54am UTC](https://forum.wixstudio.com/t/random-strip-background-on-homepage-from-a-collection-of-images/13118/6 "2021-04-07T06:54:10Z")

</div>

Was this ever answered for you? Thanks.
