I am trying to make a button that when clicked prints in a text filed input (id="textName") a random name from an Array of names. No problem so far!
After that, all the names need to sum to each other in another texfield (id="addName") and update every click adding the new name. Like having a list of names separated by a coma fiat, bmw, masaserati, ferrari).
this is my code so far.
<input type="button" id="randomName" value="name" style="position:relative; top:200px; left:50px;" />
<input type="text" id="textName" value="" style="position:relative; top:200px; left:100px; width:200px;" />
<input type="text" id="addName" value="" style="position:relative; top:300px; left:0px; width:80%;" />
<script >
var Names = ["Saab", "Volvo", "BMW", "Fiat", "Ferrari", "Maserati", "Audi"];
document.getElementById('randomName').onclick = function() {
//add code here
var NumberRandom = Math.floor((Math.random() * 7) + 1);
var allNames = Names[NumberRandom];
$('#textName').val(allNames);
console.log(NumberRandom);
for(var i=0; i<NumberRandom; i++){
var total = Names[NumberRandom] += 1 ;
console.log(total);
$('#addName').val(total);
return;
}
}
</script>
here the fiddle:
http://jsfiddle.net/salvonostrato/m2Upg/
Any Idea how to have sum the names every click separated by coma in the addName input?