Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do you generate random text in Javascript?

I want to make a button that randomly chooses a quote from an array and displays it. The button is an image.

1 Answer

Relevance
  • Robert
    Lv 6
    8 years ago
    Favorite Answer

    You could use methods like in code below:

    <html>

    <head>

    <script>

    var arr = ["quote 1", "quote 2", "quote 3", "quote 4"];

    function pickquote() {

    var textinput = document.getElementById('textinput');

    textinput.value = arr[ Math.floor(( Math.random() * arr.length) + 0)];

    return textinput.value;

    }

    </script>

    </head>

    <body>

    <input type='text' id='textinput' size=80></input>

    <button type='button' onclick='pickquote();'>Please pick a quote</input>

    </body>

    </html>

    <!-- spaceId: 2114709203 -->

    <!-- spaceId: 2114709203 -->

Still have questions? Get your answers by asking now.