I got a script that picks a random word from an array. What I want this to do is to print one of these arrays into a textarea after the user clicks a button.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var myArray = [
"Test",
"Work",
"Life"
];
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
document.body.innerHTML = randomItem;
</script>
</body>
</html>
You could select your text area with some javascript methods such as document.getElementById, document.querySelector and set its value.
const myArray = ["Test", "Work", "Life"];
// const textArea = document.getElementById('my-textarea');
const textArea = document.querySelector('#my-textarea');
function updateTextArea() {
var randomItem = myArray[Math.floor(Math.random() * myArray.length)];
textArea.value = randomItem;
}
<textarea name="" id="my-textarea" cols="30" rows="10"></textarea>
</br>
<button onclick="updateTextArea()">Update Textarea</button>