Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocodesupplyco/d95d492eba113ef5ce656c5b4c0e432f to your computer and use it in GitHub Desktop.
Save nocodesupplyco/d95d492eba113ef5ce656c5b4c0e432f to your computer and use it in GitHub Desktop.
Randomize Button Text on Click
// Get element with ID of "btn" to detect click and randomize text
document.getElementById("btn").onclick = function () {
getrandom();
};
// Arrary of text options to randomize with
const arr = ["apple", "orange", "pineapple", "banana", "grapes"];
// Function to do the randomizing
function getrandom() {
const randomValue = arr[Math.floor(Math.random() * arr.length)];
document.getElementById("btn").textContent = randomValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment