# Code only works on first page of pagination

**URL:** <https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572>\
**Category:** Attributes\
**Created:** [May 11, 2023, 4:56pm UTC](https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572 "2023-05-11T16:56:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenjhilario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.finsweet.com/stevenjhilario/32/81_2.png) [@stevenjhilario](https://forum.finsweet.com/u/stevenjhilario)\
**Post date:** [May 11, 2023, 4:56pm UTC](https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572/1 "2023-05-11T16:56:06Z")

</div>

I have added a custom code to set a random background image to a div. This code works but only on the first page of the collection since Im using pagination attributes. How can I fix this?

read-only link:  
[https://preview.webflow.com/preview/burtch-works?utm\_medium=preview\_link&utm\_source=designer&utm\_content=burtch-works&preview=ef3f109a25f71f839d3acd6c8bf719fa&pageId=645a3bc8aa55ed6f95e5f6fa&workflow=preview](https://preview.webflow.com/preview/burtch-works?utm_medium=preview_link&utm_source=designer&utm_content=burtch-works&preview=ef3f109a25f71f839d3acd6c8bf719fa&pageId=645a3bc8aa55ed6f95e5f6fa&workflow=preview)

---

<div class="post-metadata">

**Author:** ![Support-Luis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.finsweet.com/support-luis/32/53_2.png) [@Support-Luis](https://forum.finsweet.com/u/Support-Luis)\
**Post date:** [May 11, 2023, 6:16pm UTC](https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572/2 "2023-05-11T18:16:20Z")

</div>

@stevenjhilario you can use the Attributes API for this, here is an example:

```auto
<script>
  window.fsAttributes = window.fsAttributes || [];
  window.fsAttributes.push([
    'cmsload',
    (listInstances) => {
      console.log('cmsload Successfully loaded!');

      // The callback passes a `listInstances` array with all the `CMSList` instances on the page.
      const [listInstance] = listInstances;

      // The `renderitems` event runs whenever the list renders items after switching pages.
      listInstance.on('renderitems', (renderedItems) => {
        console.log(renderedItems);
        // Add code that will run each time new items are rendered on the page
      });
    },
  ]);
</script>

```

For your particular case, you can replace the code on your page with this:

```auto
<script>
  const backgroundImages = [
    'https://assets.website-files.com/642f06aad4855fdea1778af3/645d0788655937ed1ecbca1e_Rotating%20Blog%20Cover%20-%206.webp',
    'https://assets.website-files.com/642f06aad4855fdea1778af3/645d0788f8f2b42feafdcf77_Rotating%20Blog%20Cover%20-%202.webp',
    'https://assets.website-files.com/642f06aad4855fdea1778af3/645d078875de33d4455ac0e7_Rotating%20Blog%20Cover%20-%205.webp',
    'https://assets.website-files.com/642f06aad4855fdea1778af3/645d0788678fda17deb31d05_Rotating%20Blog%20Cover%20-%2010.webp',
    'https://assets.website-files.com/642f06aad4855fdea1778af3/645d0788862633c197de77ba_Rotating%20Blog%20Cover%20-%204.webp',
    // Add more image URLs or filenames as needed
  ];

  function applyRandomBackground() {
    const randomBackgrounds = document.querySelectorAll('.random-background');

    randomBackgrounds.forEach((randomBackground) => {
      const randomIndex = Math.floor(Math.random() * backgroundImages.length);
      const randomImage = backgroundImages[randomIndex];

      randomBackground.style.backgroundImage = `url('${randomImage}')`;
    });
  }

  window.fsAttributes = window.fsAttributes || [];
  window.fsAttributes.push([
    'cmsload',
    (listInstances) => {
      // Apply random background when the page loads initially
      applyRandomBackground();

      // The callback passes a `listInstances` array with all the `CMSList` instances on the page.
      const [listInstance] = listInstances;

      // The `renderitems` event runs whenever the list renders items after switching pages.
      listInstance.on('renderitems', (renderedItems) => {
        // Apply random background when new items are rendered
        applyRandomBackground();
      });
    },
  ]);
</script>

```

---

<div class="post-metadata">

**Author:** ![stevenjhilario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.finsweet.com/stevenjhilario/32/81_2.png) [@stevenjhilario](https://forum.finsweet.com/u/stevenjhilario)\
**Post date:** [May 16, 2023, 4:41pm UTC](https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572/3 "2023-05-16T16:41:25Z")

</div>

Thank you! this worked!

---

<div class="post-metadata">

**Author:** ![BillVDH](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.finsweet.com/billvdh/32/4395_2.png) [@BillVDH](https://forum.finsweet.com/u/BillVDH)\
**Post date:** [May 9, 2024, 6:37pm UTC](https://forum.finsweet.com/t/code-only-works-on-first-page-of-pagination/572/4 "2024-05-09T18:37:23Z")

</div>

Hey @Support-Luis  
I have an interesting use case for this - I have utilized the code you provided successfully on other projects but I have a fringe case that does not seem to work

I am circumventing the 5 item limit by using jquery load within the list (Teams \> Recruited Players relationship) and they in an accordion

Everything works great, However only on the first page and I cannot seem to get it to trigger after the pagination this time

Below is a screenshot so you can see the basic structure as well as my current code implementation (uses dynamic data in an embed)

_\> Also I went this route because CMS Nest would have to render about 300 players list on the page if I used the attribute method for nesting - unless you can think of a way around that_

 ![CleanShot 2024-05-09 at 13.00.54@2x](https://europe1.discourse-cdn.com/flex013/uploads/finsweet1/original/2X/d/da2de7d9f30f4e6a8b2a7585f84a35436333ba8d.png)  
 ![CleanShot 2024-05-09 at 13.33.01@2x](https://europe1.discourse-cdn.com/flex013/uploads/finsweet1/original/2X/6/608fe4cab62b512ca916b1fb67f6bfd7f2cb806a.png)

```auto
<div class="players-list" id="players-{{SLUG}}></div>

<script>
window.addEventListener('DOMContentLoaded', function() {
   jQuery(function() {
       jQuery('#players-{{SLUG}}').load("/teams/{{SLUG}} .team-table_list")
   });
});
</script>

```

🙏 🙏 🙏 🙏
