CMS-Driven Accessible Modals (Run js before Accessible Modal Attribute)

Hello, I have multiple Accessible Modals on a page in a cms list and I would like to run some code prior to Attributes loading so that I can dynamically set the various attribute values (open-3, close-3, etc.)

Is there something I can listen for in my code or a way I can delay the Accessible Modal setup so as it only runs once my code has finished updating values?

For anyone having this issue, here was the solution ie. Add the Attributes embed script after running your code to add incremented attribute values.

window.onload = function() {
  // Select the parent element
  let parent = document.querySelector('.team-list_list');
  // Select all the child elements
  let items = parent.querySelectorAll('.team-list_lightbox-item');
  // Iterate over each item
  items.forEach((item, index) => {
    // We increment the index because it starts from 0
    let counter = index + 1;

    // Find the specific child elements within each item where you want to apply the attributes
    let buttonElement = item.querySelector('.team-list_modal-2_button');
    let coverElement = item.querySelector('.team-list_modal-2_cover');
    let popupElement = item.querySelector('.team-list_modal-2_popup');
    let closeElement = item.querySelector('.team-list_modal-2_close');
    // Add the attribute to the target elements
    if (buttonElement) buttonElement.setAttribute('fs-modal-element', `open-${counter}`);
    if (coverElement) coverElement.setAttribute('fs-modal-element', `close-${counter}`);
    if (closeElement) closeElement.setAttribute('fs-modal-element', `modal-${counter}`);
    if (popupElement) popupElement.setAttribute('fs-modal-element', `close-${counter}`);
  //[Finsweet Attributes] Modal 
  (()=>{var t="",e=document.querySelector(`script[src="${t}"]`);e||(e=document.createElement("script"),e.async=!0,e.src=t,document.head.append(e));})();