# Creating Random Order of DB List - Javascript

**URL:** <https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [November 9, 2021, 4:51pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710 "2021-11-09T16:51:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DecodeCreate](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/decodecreate/32/191873_2.png) [@DecodeCreate](https://community.hubspot.com/u/DecodeCreate)\
**Post date:** [November 9, 2021, 4:51pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/1 "2021-11-09T16:51:34Z")

</div>

I’m brand new to Hubspot and HubL in general.

I’m trying to randomize the way a list appears after it’s been built from a HubL Db. I’m struggling to integrate the javascript into the HubL markup workflow. What I’m trying to do is create a list that reorders itself randomly anytime the page is refreshed.

I understand that built into HubL are filters (random, shuffle) but neither of those produces the results I’m looking for.

Below is my markup;

```lia-code-sample
{% set table = hubdb_table_rows(5296993) %}

{% if table == [] %}
<p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
<ul>
 {% for row in table %}
 <li>
 <a href="/brands/{{ row.hs_path }}">
 <img src="{{ row.logo_bw.url }}" width="500" {% if row.image.width>row.image.height %}class="landscape"{% endif %} alt="Logo for {{ row.name }}">
 </a>
 </li>
 {% endfor %}
</ul>
{% endif %}

```

Any assistance would be greatly appreciated. Thank you.

---

<div class="post-metadata">

**Author:** ![Oezcan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oezcan/32/53959_2.png) [@Oezcan](https://community.hubspot.com/u/Oezcan)\
**Post date:** [November 9, 2021, 6:05pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/2 "2021-11-09T18:05:10Z")

</div>

Hey @DecodeCreate ,  
try this:

{% set rand\_table = table|random %}  
\<ul\>  
{% for row in rand\_table %}  
\<li\>  
\<a href=“/brands/{{ row.hs\_path }}”\>  
\<img src=“{{ row.logo\_bw.url }}” width=“500” {% if row.image.width\>row.image.height %}class=“landscape”{% endif %} alt=“Logo for {{ row.name }}”\>  
\</a\>  
\</li\>  
{% endfor %}  
\</ul\>  
{% for row in rand\_table %}

And I wouldn’t recommend mixing Hubl with js.  
If you want to do pure javascript / jquery it works. Then just give li a class and mix it with jquery.  
Best regards  
Özcan

---

<div class="post-metadata">

**Author:** ![DecodeCreate](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/decodecreate/32/191873_2.png) [@DecodeCreate](https://community.hubspot.com/u/DecodeCreate)\
**Post date:** [November 9, 2021, 7:02pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/3 "2021-11-09T19:02:07Z")

</div>

Thanks Ozcan,

I tried what you suggested earlier - but it breaks the link between the database and the loop. I’m no longer pulling any data…you can see from my screenshot below.

 ![DecodeCreate_0-1636484140256.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/8/b/8b98cd84a68e0370f0bf2d1871158bbf45ed29fd.png)

The HubL documentation on the [filters page](https://developers.hubspot.com/docs/cms/hubl/filters) is the one that told me I should use javascript to randomize the table rows…

 ![DecodeCreate_1-1636484343424.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/6/6f10de02a382c5e5d900c3aac0f60d3c2f3dae23.png)

Any thoughts here?

Once again I really do appreciate your time and help as I’m totally new in the Hubspot / HubL realm.

Best,

Brandon

---

<div class="post-metadata">

**Author:** ![Oezcan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oezcan/32/53959_2.png) [@Oezcan](https://community.hubspot.com/u/Oezcan)\
**Post date:** [November 9, 2021, 7:37pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/4 "2021-11-09T19:37:06Z")

</div>

Hey @DecodeCreate all good, that’s what we’re here for.

Have you already tried it?

> <https://stackoverflow.com/questions/14555415/how-to-randomly-sort-list-items/14555430#14555430>

Best regards  
Özcan

---

<div class="post-metadata">

**Author:** ![DecodeCreate](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/decodecreate/32/191873_2.png) [@DecodeCreate](https://community.hubspot.com/u/DecodeCreate)\
**Post date:** [November 9, 2021, 7:45pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/5 "2021-11-09T19:45:03Z")

</div>

I did – the screenshot I sent you was after I implemented the {% set rand\_table = table|random %} to the markup block.  
I’ll look into those JQuery blocks you sent me. Thanks again for the assistance.

---

<div class="post-metadata">

**Author:** ![DecodeCreate](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/decodecreate/32/191873_2.png) [@DecodeCreate](https://community.hubspot.com/u/DecodeCreate)\
**Post date:** [November 10, 2021, 2:44pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/6 "2021-11-10T14:44:58Z")

</div>

Good morning Özcan,

Wanted to drop you a note to let you know I found a simple solution to the issue. It’s actually really simple.

**Solution**

**HTML**

```lia-code-sample
{% if table == [] %}
<p class='align-center'>Sorry, no one found for that Search. Try changing your filter and search again.</p>
{% else %}
<ul class='brand-list'>
 {% for row in table %}
 <li>
 <a href="/brands/{{ row.hs_path }}">
 <img src="{{ row.logo_bw.url }}" width="500" {% if row.image.width>row.image.height %}class="landscape"{% endif %} alt="Logo for {{ row.name }}">
 </a>
 </li>
 {% endfor %}
</ul>
{% endif %}

```

**Javascript**

```lia-code-sample
var shuffle = document.querySelector('ul.brand-list');
for(var i = shuffle.children.length; i >= 0; i--) {
 shuffle.appendChild(shuffle.children[Math.random() * i | 0]);
}

```

It’s based on a tutorial that I found [here](https://javascript.tutorialink.com/javascript-shuffle-html-list-element-order/) and works really slick.

Thanks again for your time.

Best

Brandon

---

<div class="post-metadata">

**Author:** ![Oezcan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/oezcan/32/53959_2.png) [@Oezcan](https://community.hubspot.com/u/Oezcan)\
**Post date:** [November 10, 2021, 2:50pm UTC](https://community.hubspot.com/t/creating-random-order-of-db-list-javascript/46710/7 "2021-11-10T14:50:38Z")

</div>

Hey @DecodeCreate ,

good Job and thank you for the note! Yeah, that is really simple!  
Best regards,  
Özcan
