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>
quotes.lengthwill be one more than the total number of items. Maybe tryquotes.length - 1? - Also,.valueisn't a valid property of a paragraph element, and even if it was, the for loop would just override the value multiple times.