Obtuve un script que elige una palabra aleatoria de una matriz. Lo que quiero que haga es imprimir una de estas matrices en un área de texto después de que el usuario haga clic en un botón.
<!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>Puede seleccionar su área de texto con algunos métodos de JavaScript como document.getElementById , document.querySelector y establecer su valor.
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>