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)

5 Answers 5

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

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

1 Comment

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!
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).

1 Comment

thanks, how would I write the code for the button to trigger the function is it something like <input onclick="myarray()" type="button" value="Click Me" id="myButton1" />
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"/>

Comments

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);
}

Comments

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)]);
};

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.