1

I have a java script array with large number of elements inside it, on click of a button I want to display the any random array element on screen, for which I have used Math.random function, but not sure why it is not working. here is my code below.

<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <button id="getquotes" value="Quotes" onclick="Loadquotes();">Quotes</button>
    <p id="quoteshere" ></p>
    <script>
      var Loadquotes= function(){
        var quotes = new Array('Stack1','Stack2','Stack16','Stack17','Stack13','Stack14','Stack15','Stack6','Stack7','Stack8','Stack9','Stack10');
        var i;

        for (i=0;i<quotes.length;i++){
          var newquotes = quotes[Math.floor(Math.random() * quotes.length)];
          document.getElementById('quoteshere').value=newquotes;
        }
      };
    </script>
  </body>
</html>

2
  • quotes.length will be one more than the total number of items. Maybe try quotes.length - 1? - Also, .value isn't a valid property of a paragraph element, and even if it was, the for loop would just override the value multiple times. Commented Feb 2, 2017 at 10:03
  • @evolutionxbox tried it..but still not working Commented Feb 2, 2017 at 10:05

3 Answers 3

2

quoteshere is P Tag value function won't work use innerText or innerHTML instead please find below snippet

var Loadquotes= function(){
  debugger;
  var quotes = new Array('Stack1','Stack2','Stack16','Stack17','Stack13','Stack14','Stack15','Stack6','Stack7','Stack8','Stack9','Stack10');
  var i;

  for (i=0;i<quotes.length;i++){
    var newquotes = quotes[Math.floor(Math.random() * quotes.length)];
    document.getElementById('quoteshere').innerText = newquotes;
  }
};
<button id="getquotes" value="Quotes" onclick="Loadquotes();">Quotes</button>
<p id="quoteshere" ></p>

Sign up to request clarification or add additional context in comments.

Comments

1

you can try this

var quotes = Array('Stack1','Stack2','Stack16','Stack17','Stack13','Stack14','Stack15','Stack6','Stack7','Stack8','Stack9','Stack10');
var Loadquotes= function(){
    var newquotes = quotes[Math.floor(Math.random() * quotes.length)];
    document.getElementById('quoteshere').innerHTML=newquotes;
};
<button id="getquotes" value="Quotes" onclick="Loadquotes();">Quotes</button>
<p id="quoteshere" ></p>

Comments

0

Try This

<html>
<head>
    <title>Demo</title>
</head>
        <body>
                <button id="getquotes" value="Quotes" onclick="Loadquotes()"> Quotes </button>
                    <p id="quoteshere" ></p>
                        <script>
                function Loadquotes(){
                var quotes = new Array('Stack1','Stack2','Stack16','Stack17','Stack13','Stack14','Stack15','Stack6','Stack7','Stack8','Stack9','Stack10');
                var newquotes = Math.floor(Math.random() * quotes.length);
                document.getElementById('quoteshere').innerHTML = quotes[newquotes];

                 }
                        </script>
        </body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.