0

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?

1
  • What do you mean by "names need to sum to each other"? Summing is usually done with numbers. Commented Jul 4, 2014 at 14:07

2 Answers 2

1

Here's a fixed version of your fiddle

var Names = ["Saab", "Volvo", "BMW", "Fiat", "Ferrari", "Maserati", "Audi"];


document.getElementById('randomName').onclick = function() {
  //add code here
  var NumberRandom = Math.floor((Math.random() * Names.length));
  var allNames = Names[NumberRandom];

  $('#textName').val(allNames);

  console.log(NumberRandom);

    var curVal = $("#addName").val()
    if (curVal.length > 0) {
        curVal += ','
    }
    curVal += allNames
    $("#addName").val(curVal)

}

Note the change in the generation of the random number (line 6) and the curVal change at the bottom (line 13-18).

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

4 Comments

Please place the answer here (linking is okay). If the link dies, there is no future reference.
is there a way to count the names? I am trying using "curVal.length" but instead it adds the number of letters together
is there a way to count the names? I am trying using "curVal.length" but instead it adds the number of letters together
You should probably use an array instead of a string if you want to count the names.
0

Please check this fiddle

var Names = ["Saab", "Volvo", "BMW", "Fiat", "Ferrari", "Maserati", "Audi"];


document.getElementById('randomName').onclick = function () {
//add code here
var NumberRandom = Math.floor((Math.random() * 6) + 1);
var allNames = Names[NumberRandom];

$('#textName').val(allNames);

console.log(NumberRandom);


for (var i = 0; i < NumberRandom; i++) {

    var total = Names[NumberRandom] += 1;
    var prev = $('#addName').val();
    console.log(prev);
    if (prev != '') {
        prev += ',' + total;
    }else{
        prev = total;
    } 

    $('#addName').val(prev);

    return;
}

}

1 Comment

You shouldn't be doing single not equals in javascript (!=). Also, for checking non-empty string, do string.length > 0

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.