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.
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
- Add the custom Javascript to the head of your page code, or via an embed.
- Utilize element IDs with btn on your selected button
- 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>