1

So now I get a ',' seperator after every time I press submit on my input field which is linked to the myFunction function. I'd like to remove the ',' seperator. Because when I submit a name now it just prints a comma and adds another comma every time I press submit without filling out the input field.

        var namenOSM = [];
        var clubOSM = ["Wolves", "Derby", "Cardiff", "Aston villa", "Bristol City", "Sheffield utd", "Fulham", "Middlesbrough", "Brentford", "Leeds", "Preston", "Ibswich", "Norwich", "Nottingham", "Millwall", "QPR", "Sheffield Wed", "Redding", "Barnsley", "Bolton", "Hull city", "Sunderland", "Birmingham", "Burton"];



        function myFunction() {

            var newArray = document.getElementById("naam").value;
            document.getElementById("naam").value = "";
            namenOSM.push(newArray);

            var clubRandom = Math.floor(Math.random() * clubOSM.length);

            var node = document.createElement("LI");
            var textnode = document.createTextNode(namenOSM + ' = ' + clubOSM[clubRandom]);

            node.appendChild(textnode);
            document.getElementById("namen").appendChild(node);

        }
    Player:<br>
    <input id="naam" type="text" name="firstname" value="">
    <br>
    <br>
    <input type="button" value="Submit" onclick="myFunction()">


    <div id="namen">

    </div>

I know the clubs are hardcoded into the array but it was a quick project for me to learn javascript a little.

Thanks!

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Ryan
  • 21
  • 4

3 Answers3

0

The issue is you are outputting the entire array instead of the last index.

var textnode = document.createTextNode(namenOSM + ' = ' + clubOSM[clubRandom]);

should be either

var textnode = document.createTextNode(namenOSM[namenOSM.length-1] + ' = ' + clubOSM[clubRandom]);

or

var textnode = document.createTextNode(newArray + ' = ' + clubOSM[clubRandom]);
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You should apply empty input control for firstname input and be careful with name convenience, newArray is not a array.

    var namenOSM = [];
    var clubOSM = ["Wolves", "Derby", "Cardiff", "Aston villa", "Bristol City", "Sheffield utd", "Fulham", "Middlesbrough", "Brentford", "Leeds", "Preston", "Ibswich", "Norwich", "Nottingham", "Millwall", "QPR", "Sheffield Wed", "Redding", "Barnsley", "Bolton", "Hull city", "Sunderland", "Birmingham", "Burton"];



    function myFunction() {

        var textValue = document.getElementById("naam").value;
        if(textValue == "")
    return;
        document.getElementById("naam").value = "";
        namenOSM.push(textValue);

        var clubRandom = Math.floor(Math.random() * clubOSM.length);

        var node = document.createElement("LI");
        var textnode = document.createTextNode(namenOSM + ' = ' + clubOSM[clubRandom]);

        node.appendChild(textnode);
        document.getElementById("namen").appendChild(node);

    }
Player:<br>
<input id="naam" type="text" name="firstname" value="">
<br>
<br>
<input type="button" value="Submit" onclick="myFunction()">


<div id="namen">

</div>
lucky
  • 12,734
  • 4
  • 24
  • 46
-1

This is if you want to keep the output exactly the same, just without the commas otherwise take a look @epascarello answer

the reason for the , is because you are printing out the object array rather then the individual element of the array. To do that you would need a for loop

https://www.w3schools.com/js/js_loop_for.asp

I added it in, so it adds all elements in the array to a string then you display that string.

for(var i = 0; i < namenOSM.length; i++){
       teamString += namenOSM[i] + " ";
}

https://codepen.io/anon/pen/LevJBR

Rich
  • 63
  • 1
  • 5