How to link a button using code

Hello, would anyone be able to guide on how to link a button on my dynamic page to a url in my database?

Essentially, if the user is on ‘page a,’ and clicks on the link, I want the button to be connected to the ‘url’ field for ‘a’ from my collection/database. I am planning to use $w.Linkablemixin to use .link and .target, so that I can get the url to open-up in the same window. Any help?

In order to open in the same window, you just need to connect the button link to the url field from your database. No coding needed for this.

If you wish the link to open in a new window, you will need to write some code.

Thanks Yoav. For some reason, it is the other way round for me. I am not sure if there is a glitch in the system, or if I am doing something wrong. Would you mind taking a quick peek at this page: The two buttons on the top are linked to the same url field in the database, but for some reason, the links are opening in a new window.

Hi rsuri.1,

I tested it and got the same thing you did, so I think maybe Yoav just got it backwards. In any case, the fix is really easy. On your dynamic page add the following line to the onReady function at the top of the page.

$w("#button1").target = "_self";	

You’ll need to replace “button1” with the name of your button. So in the end your code should look like this.

$w.onReady(function () {
	$w("#yourButtonName").target = "_self";	
});

And that’s it. You can then still use the Connect panel to make your button link to the URL in your collection. All this code does is tells the button that it should open any links in the same tab.

2 Likes

OMG! Thank you so much… I was way overthinking the entire thing for the past 3 days. I appreciate your help… you just saved me hours/days worth of repetitive work.

1 Like

Hi

can you assist me - i created a database with a url in it. (pic1)

i want people to login and then click on my profile (pic2) and take them to the link in their database.

i used the code -

}
export function button3_onclick() {
wixLocation.to(/Clients/Update/${wixUsers.currentUser.catalog});
}

but it doesnt work