0

The game in this example has a variable number of players. A score needs to be tracked for each player. The example managed to create the HTML needed, but I'm not sure how to reference each child. Here's the HTML:

var players=prompt("Number of players?",2);

oldNode = document.getElementById("scoreHolder");

for (x=1;x<players;x++) {
  newNode = oldNode.cloneNode(true);
  document.documentElement.appendChild(newNode); 
}

var c=document.getElementById("scoreHolder").childNodes;
c[1].style.backgroundColor = "yellow";
c[2].style.backgroundColor = "blue";
<div id="scoreHolder">Player 1 score: <span id="score">0<span></div>

This example turns part of the first line of HTML yellow, but does nothing with the other lines. How can I reference them so that I can update each player's score?

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • I want ask one question : Do you know from what index starts Array ? – xAqweRx May 04 '15 at 08:51
  • @xAqweRx Player #1 is already in the DOM so it can be skipped. – JJJ May 04 '15 at 08:53
  • I wanted to tell you, thar Array in JS starts form 0 index, not from 1 index. – xAqweRx May 04 '15 at 08:54
  • @xAqweRx And I wanted to tell you that the program would not work correctly if you started looping from 0. – JJJ May 04 '15 at 08:55
  • Then provide more HTML code. because I can't see it. – xAqweRx May 04 '15 at 08:58
  • 1
    Yes, agreed that arrays start at 0. This isn't a typical case, though. Player 1 is already included. We need to add (length-1) elements. We could start counting at 1 or we could stop when we reach length-1. – Mike Risher May 04 '15 at 08:59
  • @xAqweRx Click on the "Run code snippet" button. When it asks for the number of players, type "4" and press ok. You'll see 4 rows appear. Why do you think there is a problem? – JJJ May 04 '15 at 09:00
  • @Juhana I'm telling that it's not correct to set manually backgorund elements with index 1 and 2 if you don't know how many of them will be added. – xAqweRx May 04 '15 at 09:07

2 Answers2

0
<body>
    <!-- add name attribute -->
    <div id="scoreHolder" name="scoreHolder">Player 1 score: <span id="score">0<span></div>
</body>
<script type="text/javascript">
    var players=prompt("Number of players?", 2);

        oldNode = document.getElementById("scoreHolder");

        for (var x=1;x<players;x++) {
          newNode = oldNode.cloneNode(true);
          newNode.id="newId"+x; // reassign the new id
          document.documentElement.appendChild(newNode);    
        }

        // use getElementsByName
        var c=document.getElementsByName("scoreHolder");
        c[0].style.backgroundColor = "yellow";
        c[1].style.backgroundColor = "blue";
</script>
Howard Wang
  • 601
  • 3
  • 18
  • Thank you. I finally see blue showing up. I'll remember to use getelementsByName next time. I also notice the clever thing you did with adding new ids. – Mike Risher May 04 '15 at 09:13
-1

For doing this use for loop :

for( var i = 0; i < c.length; i++ ){
   if( i%2 == 1 )
       node[i].style.backgroundColor = "yellow";
   else    
       node[i].style.backgroundColor = "blue";
}

Update I've made a mistake with indexes. try updated one


UPDATE 2

You have a problem with function document.getElementById("scoreHolder") - it returns only one element, so try add class to #scoreHolder and document.getElementsByClassName and delete ID from appended elements

xAqweRx
  • 1,236
  • 1
  • 10
  • 23