Code

Randomize button text on click

This allows you to randomize the text on a button click in Webflow. A fun way of displaying a variety of different texts when a user clicks on a button on your Webflow page.

Randomize button text on click preview

This allows you to randomize the text on a button click in Webflow. A fun way of displaying a variety of different texts when a user clicks on a button on your Webflow page.

How to add it

  1. Add the custom Javascript to the head of your page code, or via an embed.
  2. Utilize element IDs with btn on your selected button
  3. Update the const arr values for the text that you'd like to randomize

The code

Javascript
                      <script>
document.getElementById("btn").onclick = function() {
  getrandom();
};

const arr = ["apple", "orange", "pineapple", "banana", "grapes"];
function getrandom() {
  const randomValue = arr[Math.floor(Math.random() * arr.length)];
  document.getElementById("btn").textContent = randomValue;
}
</script>

                    
HTML
                      <button id="btn">apple</button>

                    

Browse similar code snippets

Language

More from Corey Moen

Profile →

Related Webflow questions

All answers →

Similar code snippets

All code snippets →