2

Please can someone help. I am trying to create a basic button in javascript that when clicked picks a value at random from my array and display it on the screen, each time the button is clicked it should pick a new item from the array. I know how to write the array

var myarray = new Array("item1", "item2", "item3");

I just dont know how to do the button part. Any help would be great. I know it may be easier to do this in jQuery, but I really want to get my head round javascript before I tackle jQuery (please be gentle I am new to this lol)

Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168
user2419017
  • 23
  • 1
  • 1
  • 5

5 Answers5

6

You can call a function on button click to print the value like this

<input type="button" id="btnSearch" value="Search" onclick="GetValue();" />
<p id="message" ></p>

JS

function GetValue()
{
    var myarray= new Array("item1","item2","item3");
    var random = myarray[Math.floor(Math.random() * myarray.length)];
    //alert(random);
    document.getElementById("message").innerHTML=random;
}

JS Fiddle Demo

Sachin
  • 40,216
  • 7
  • 90
  • 102
  • Thank you very much everyone who has contributed an answer. They have been a big help for me to understand what is going on with this. Thanks again! – user2419017 May 25 '13 at 21:24
0

Use Random function of javascript to generate a random number between upper and lower limit of your array. Then use

myarray[Math.round(Math.random() * (myarray.length - 1))]

to access a random value from the array.

You can use this link to see hot to generate a random number between a min and max number. (for your case min will always be 0).

Community
  • 1
  • 1
Guanxi
  • 3,103
  • 21
  • 38
0

Try this one: HTML:

<input type="button" click="randomize()" value="Click me" />

JS:

function randomize(){
  var myarray = new Array("item1", "item2", "item3");
  var randomId =  Math.floor((Math.random()*myarray.length));
  var randomItem = myarray[randomid];

  alert(randomItem);
}
Matt
  • 74,352
  • 26
  • 153
  • 180
Epsil0neR
  • 1,676
  • 16
  • 24
0

http://jsfiddle.net/McKxp/

JS:

var arr = ["foo","bar","baz"];

function getItem(){
    document.getElementById("something").innerHTML = arr[Math.floor(Math.random() * arr.length)];
}

HTML

<div id="something"></div>
<input type="button" onclick="getItem()" value="Click me"/>
aquinas
  • 23,318
  • 5
  • 58
  • 81
0

Here is an example:

http://jsfiddle.net/kUgHg/2/

The steps are:

  1. Create a button element and give it an ID:

    <button id="button1">Show random item</button>
    
  2. Get a reference to the button in your script:

    document.getElementById("button1");
    
  3. Compute a random index in your array based on the array length:

    Math.floor(Math.random() * array.length)
    
  4. Add a click event handler to your button that calls the random computation at step 3 and shows it a way you want:

    button.onclick = function() { … }
    

All in one:

var button = document.getElementById("button1");
var myarray = ["a", "b", "c", "d"];

button.onclick = function() {
    alert(myarray[Math.floor(Math.random() * myarray.length)]);
};
Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168