# Dynamically Change URL of a button from CMS Fields

**URL:** <https://forum.wixstudio.com/t/dynamically-change-url-of-a-button-from-cms-fields/55340>\
**Category:** Ask the community\
**Tags:** cms, code\
**Created:** [January 11, 2024, 10:30pm UTC](https://forum.wixstudio.com/t/dynamically-change-url-of-a-button-from-cms-fields/55340 "2024-01-11T22:30:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Hays](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/john_hays/32/101348_2.png) [@John\_Hays](https://forum.wixstudio.com/u/John_Hays)\
**Post date:** [January 11, 2024, 10:30pm UTC](https://forum.wixstudio.com/t/dynamically-change-url-of-a-button-from-cms-fields/55340/1 "2024-01-11T22:30:53Z")

</div>

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.

> **[Portfolio | Miramonte Homes](https://www.miramontehomes.com/portfolio)**
>
> For over three decades, Miramonte has stood as a beacon of excellence in the realm of real estate. As a premier builder, we have shaped not just residential communities, but also rental havens and thriving commercial properties. Our unwavering...

---

<div class="post-metadata">

**Author:** ![anthony](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/anthony/32/79536_2.png) [@anthony](https://forum.wixstudio.com/u/anthony)\
**Post date:** [January 12, 2024, 4:18pm UTC](https://forum.wixstudio.com/t/dynamically-change-url-of-a-button-from-cms-fields/55340/2 "2024-01-12T16:18:52Z")

</div>

Moved to #Collaboration. [wix.com/marketplace](http://wix.com/marketplace) is also a good place to check if you need freelance coding assistance.

---

<div class="post-metadata">

**Author:** ![is7](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@is7](https://forum.wixstudio.com/u/is7)\
**Post date:** [January 13, 2024, 1:08am UTC](https://forum.wixstudio.com/t/dynamically-change-url-of-a-button-from-cms-fields/55340/3 "2024-01-13T01:08:37Z")

</div>

You can do this pretty easily

```javascript
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.
