Dynamically Change URL of a button from CMS Fields

Hi all,

Looking for code that will change the URL of a button within a repeater. I have two fields in my collection one is default URL and another for alternate URL. I want to tell the button to use the alternate URL if data exists in that field, otherwise use the default URL. Can someone help with a little code for this? Thanks so much!

Product:
Editor X

This is the page, and it’s the “View More” button within the repeater section.
https://www.miramontehomes.com/portfolio

Moved to Collaboration. wix.com/marketplace is also a good place to check if you need freelance coding assistance.

You can do this pretty easily

function setRepeater (data) {
      $w("#myRepeater").data =data
      $w("#myRepeater").onItemReady( ($item, itemData, index) => {
        if (itemData.dataForURL1) { 
          $item("myButton").link = itemData.url1
        } else {
          $item("myButton").link = itemData.url2
        }
      });
}

This should be called after you perform a wixData query to retrieve your data. Pass the items from the query response to this function and it should set your repeater. Note you will also need to attach the data for all other elements (ex. $item(“myImage”).src = itemData.imageToShow) of the repeater should you opt for this solution.

1 Like