# Display A Random Text?

**URL:** <https://forum.wixstudio.com/t/display-a-random-text/40646>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [June 19, 2022, 10:28am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646 "2022-06-19T10:28:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mysterypotatoheads](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mysterypotatoheads/32/14703_2.png) [@mysterypotatoheads](https://forum.wixstudio.com/u/mysterypotatoheads)\
**Post date:** [June 19, 2022, 10:28am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/1 "2022-06-19T10:28:07Z")

</div>

So I’ve been looking through tutorials and they all just don’t work. I just can’t figure this out. I’m trying to make it so every time you visit/refresh a certain page, it shows different text.

This is the code I’m trying to use from these tutorials- It’s not showing any errors, but it’s not working.

**import** wixData **from** ‘wix-data’ ;  
**let** randomActivate = **true** ;

wixData . query ( ‘dataset1’ ). limit ( 25 ). find (). then ( ( res ) =\> {  
**let** items = res . items ;  
**let** randomNum = Math . floor ( Math . random () \* items . length ) - 25 ;  
**let** randomItem = items [randomNum];  
console . log ( randomItem );  
$w ( “#text213” ). text = randomItem . value  
});

Any help with this?

---

<div class="post-metadata">

**Author:** ![wix-expert-velo-cert](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/wix-expert-velo-cert/32/2187_2.png) [@wix-expert-velo-cert](https://forum.wixstudio.com/u/wix-expert-velo-cert)\
**Post date:** [June 19, 2022, 2:35pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/2 "2022-06-19T14:35:16Z")

</div>

Hi, Why you’re substracting the 25 from the random number? Because by this way you can get a negative value. Which unable to get the item. Also, If you don’t getting an error, then try:

1. Add console.log() to check the query items.

2. Add console to check the random value.

3. Check you’re getting the random item in console.

4. Assing string value to the text. Use .toString()

If still unresolved, I request you to send the code & console screenshot.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 19, 2022, 2:55pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/3 "2022-06-19T14:55:15Z")

</div>

```javascript
 let randomNum = Math.floor(Math.random() * items.length);

```

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 19, 2022, 3:00pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/4 "2022-06-19T15:00:41Z")

</div>

Meaning, get rid of the -25

---

<div class="post-metadata">

**Author:** ![mysterypotatoheads](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mysterypotatoheads/32/14703_2.png) [@mysterypotatoheads](https://forum.wixstudio.com/u/mysterypotatoheads)\
**Post date:** [June 20, 2022, 1:59am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/5 "2022-06-20T01:59:51Z")

</div>

@jonatandor35 Even after doing that it’s not working. It just keeps giving me the same line of text.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [June 20, 2022, 2:01pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/6 "2022-06-20T14:01:07Z")

</div>

@mysterypotatoheads Be sure there’s a value for this item + check the console for errors

---

<div class="post-metadata">

**Author:** ![mysterypotatoheads](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mysterypotatoheads/32/14703_2.png) [@mysterypotatoheads](https://forum.wixstudio.com/u/mysterypotatoheads)\
**Post date:** [June 21, 2022, 1:16am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/7 "2022-06-21T01:16:41Z")

</div>

@jonatandor35 There are no errors in console. How do I check for a value of the item??

---

<div class="post-metadata">

**Author:** ![mysterypotatoheads](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mysterypotatoheads/32/14703_2.png) [@mysterypotatoheads](https://forum.wixstudio.com/u/mysterypotatoheads)\
**Post date:** [June 21, 2022, 7:40am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/8 "2022-06-21T07:40:01Z")

</div>

Got it working!

---

<div class="post-metadata">

**Author:** ![prestonkelpiek9](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/prestonkelpiek9/32/32720_2.png) [@prestonkelpiek9](https://forum.wixstudio.com/u/prestonkelpiek9)\
**Post date:** [June 21, 2022, 1:02pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/9 "2022-06-21T13:02:34Z")

</div>

@mysterypotatoheads would you mind sharing the working code?

---

<div class="post-metadata">

**Author:** ![noahlovell](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/noahlovell/32/118920_2.png) [@noahlovell](https://forum.wixstudio.com/u/noahlovell)\
**Post date:** [June 21, 2022, 2:18pm UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/10 "2022-06-21T14:18:33Z")

</div>

@prestonkelpiek9 This is the code that was used:

```javascript
import wixData from 'wix-data';

$w.onReady(function () {
        wixData.query("DatabaseID")
            .find()
            .then((result) => {
                const shuffledArray = shuffleArray(result.items);
                console.log(shuffledArray)
                $w('#textID').text = String(shuffledArray[0].title);
            })
            .catch((err) => {
                let errorMsg = err;
            });
});

function getRandomIndex(min, max) {
    return Math.round(Math.random() * (max - min) + min);
}

function shuffleArray(dataArray) {

    for (let i = dataArray.length - 1; i > 0; i--) {
        let index = getRandomIndex(0, i);
        const temp = dataArray[i];
        dataArray[i] = dataArray[index];
        dataArray[index] = temp;
    }

    return dataArray;
}

```

You will need to change DatasbaseID to the ID of your database. And textID will need to change to the ID of the text you want to display the random text within.

---

<div class="post-metadata">

**Author:** ![prestonkelpiek9](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/prestonkelpiek9/32/32720_2.png) [@prestonkelpiek9](https://forum.wixstudio.com/u/prestonkelpiek9)\
**Post date:** [June 22, 2022, 11:23am UTC](https://forum.wixstudio.com/t/display-a-random-text/40646/11 "2022-06-22T11:23:38Z")

</div>

@noahlovell thankyou!
