How to make a random link button?

I’ve looked on other posts and have found answers to this question but none of them have ever worked. My main problem is that the button will link to one particular website in my list. Does anyone have any ideas? If so, could you please help? Below is the code that I have already found.

1 Like

setTimeout(function() {$w(‘#button1’).link = allUrls[randomIndex]}, 100) might work

1 Like

Thanks for the help but that still didn’t work

Hi MLG Vids,
Since you are running this logic in the onReady function, what you actually doing is running it once. so the logic of putting a random link is fine, however it will only run once the page is loaded and never again.

I would suggest one of two ways:

  1. putting the logic inside the button onClick event, and using wixLocation.to to navigate to random URL, which will ensure it will “reRoll” again after each click.
  2. change the button link property onMouseIn event. which don’t help with keyboard navigation but its just another option you can mess around with.

Hope that helps!

1 Like

@oscarfrederiksen24 ^This, and also I meant to put setInterval, not setTimeout my bad!

2 Likes

@Lior Wiseman Im not very good with code and don’t really understand your answer. I’ve tried to do it and it does change the website but only every 2-4 times. Could you please help me to understand and/or write me an example line of code?

2 Likes

For me it says that “randomIndex is not defined”.
What should i do?

1 Like

nevermind i fixed it