I have 3 text inputs, and also have one hidden input with some numbers as its value: <input type="hidden" name="" value="100,101,105,106,109,2">.
Now I'd like to do this - when you click on 'Go' button it will randomly show one of those numbers as input values so all inputs will have value after clicking Go button. How can I do this ?
Asked
Active
Viewed 80 times
-2

aiddev
- 1,409
- 2
- 24
- 56
-
2**1** array of numbers **2** setInterval **3.** Random number generation in bounds of array.length **4.** Update value of textbox. – Tushar Oct 15 '15 at 11:42
-
Follow Tushar's steps, produce code and show it to us if you're in a trouble. – Amessihel Oct 15 '15 at 11:43
-
[Set the value of a input field with javascript](http://stackoverflow.com/questions/7609130/set-the-value-of-a-input-field-with-javascript) – WhiteHat Oct 15 '15 at 11:43
-
1is it http://jsfiddle.net/sherali/pm9vunuu/ – Sherali Turdiyev Oct 15 '15 at 11:46
-
Where is the code attempt to solve this? – charlietfl Oct 15 '15 at 11:48
-
@Sherali Turdiyev it is a bit like to what I want. Just now with your code same value can appear twice in 2 or 3 input fields, can we fix this ? – aiddev Oct 15 '15 at 11:48
1 Answers
1
Just use following code: Demo
$("#goBtn").click(function () {
var arr = $("#values").val().split(",");
var random = arr.splice(parseInt(Math.random()*arr.length), 1)[0];
$("#num1").val(random);
random = arr.splice(parseInt(Math.random()*arr.length), 1)[0];
$("#num2").val(random);
random = arr.splice(parseInt(Math.random()*arr.length), 1)[0];
$("#num3").val(random);
return false;
});

Sherali Turdiyev
- 1,745
- 16
- 29