I've been trying to randomly select an item from an array and displaying the chosen array in a form field. I've found how to display the item in a paragraph by using <p id="item"> </p>
(might be incorrect) but can't figure out how to display it in a form field :/ Help, please.
Asked
Active
Viewed 61 times
-3

charlietfl
- 170,828
- 13
- 121
- 150

user3650090
- 9
- 1
- 1
- 3
-
What do you mean by "display in form field"? What code have you tried, what did you except to get, what did you actually get? – T0xicCode May 18 '14 at 16:09
-
1Could you maybe post your current code in a http://jsfiddle.net/? – Tholle May 18 '14 at 16:09
-
http://stackoverflow.com/a/1527820/838733 – nietonfir May 18 '14 at 16:10
-
I'll use jsfiddle to show you my attempt to doing this. – user3650090 May 18 '14 at 16:11
-
http://jsfiddle.net/Tc4Pm/ – user3650090 May 18 '14 at 16:16
1 Answers
0
You can get a random value like this
var rand = myArray[Math.floor(Math.random() * myArray.length)];
Then you can display it in the form field by using
document.getElementById('id1').value = rand
where id1
is the id of your formfield

Ad Fundum
- 665
- 6
- 22