We'll continue building our table of stats on the Colombia team, but we'll do it more efficiently with javascript by using arrays and loops.
<tbody>
tag. ie. delete all the data and leave only the heading.HTML
<table> <thead> <tr> <th>Name</th> <th>Surname</th> <th>Nationality</th> <th class="age">Age</th> <th>Position</th> </tr> </thead> <tbody> </tbody> </table>
CSS
td, th { border-width: 1px; border-style: solid; } td { padding-left: 10px; padding-right: 10px; padding-top: 10px; padding-bottom: 10px; } table { border-collapse: collapse; } #carlos { font-style: italic; } .age { background-color: yellow; } #carlos .age { font-weight: 900; } td.age { font-size: 150%; }
[]
ospinaRemirez = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero'];
=
means we are assigning a value to the variable 'ospinaRemirez'.=
is [
so the value must be an array.,
and ends with ]
.;
.statsRow = "<td>" + ospinaRamirez[0] + "</td>";
"
symbols.0
, so the name is referred to as ospinaRamirez[0]
.+
symbol.statsRow = statsRow + "<td>" + ospinaRamirez[1] + "</td>";
statsRow
(new string) = statsRow
(old string) + additional elements.statsRow += "<td>" + ospinaRamirez[2] + "</td>"; statsRow += "<td>" + ospinaRamirez[3] + "</td>"; statsRow += "<td>" + ospinaRamirez[4] + "</td>";
+=
additional elements, but it means the same thing. We could have written the third line the same way, but it's good to see the different options so you can choose one you like.// add row to table
//
which means it's a comment. It tells us what the next bit of the program does, but your computer doesn't read this information. We now know that the next section of code is meant to add the row to our table.tableBody = document.getElementsByTagName("tbody")[0];
<tbody>
in the document (which is our page).<tbody>
in the page, so getElementsByTagName
returns a list and we need to identify which item in the list we want. In this case, it's the first so we use [0]
.tableBody
so we can refer to it in subsequent lines of code.newRow = tableBody.insertRow(0);
<tbody>
tag) of our tableBody element.newRow
newRow.innerHTML = statsRow;
newRow
..innerHTML
refers to the HTML code inside the element before the .
, so we're now talking about the code inside our new row.=
so we're going to assign something to the new row.statsRow
which is the string containing all the data cells.<tbody>
.ospinaRamirez = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero']; statsRow = "<td>" + ospinaRamirez[0] + "</td>"; statsRow += "<td>" + ospinaRamirez[1] + "</td>"; statsRow += "<td>" + ospinaRamirez[2] + "</td>"; statsRow += "<td>" + ospinaRamirez[3] + "</td>"; statsRow += "<td>" + ospinaRamirez[4] + "</td>"; // add row to table tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(0); newRow.innerHTML = statsRow;
players = [ ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero'], ['Carlos', 'Eccehomo Cuesta Figueroa', 'Colombia', 22, 'Defensa'], ['Juan Guillermo', 'Cuadrado Bello', 'Colombia', 33, 'Centrocampista'], ['Radamel Falcao', 'García Zárate', 'Colombia', 35, 'Delantero'] ];
,
.;
is at the end of the entire statement.players
.players[0]
, players[1]
, players[2]
and players[3]
. Rather than writing out each of these in turn, we can use a javascript loop as follows.// loop through all players for (p = 0; p < players.length; p++) { }
{}
as many times as needed. We define how many times with the structure before the {
.for
loop, using p
as our iterator.()
we can see three code items separated by ;
.p = 0
tells the computer where to start - by assigning variable p a value of 0.p < players.length
is true. Recall that the element after the .
belongs to the element before the .
, so here players.length
is the length of the array called players, which is 4. So the loop should run while p < 4, ie 0, 1, 2 and 3.players[0]
is the same as ospinaRamirez.// create stats string statsRow = "<td>" + players[p][0] + "</td>"; statsRow += "<td>" + players[p][1] + "</td>"; statsRow += "<td>" + players[p][2] + "</td>"; statsRow += "<td>" + players[p][3] + "</td>"; statsRow += "<td>" + players[p][4] + "</td>";
{}
tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(p); newRow.innerHTML = statsRow;
0
after insertRow
to p
. If we leave it as 0
we'll be adding the new line at the top each time. By putting it as the index of the player in the list, the list on screen will be the same as the list in our array.tableBody
doesn't ever change, so there's no need to reassign it every time we go through the loop. Let's move that line above the loop so it's calculated first and never again.players = [ ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero'], ['Carlos', 'Eccehomo Cuesta Figueroa', 'Colombia', 22, 'Defensa'], ['Juan Guillermo', 'Cuadrado Bello', 'Colombia', 33, 'Centrocampista'], ['Radamel Falcao', 'García Zárate', 'Colombia', 35, 'Delantero'] ]; tableBody = document.getElementsByTagName("tbody")[0]; // loop through all players for (p = 0; p < players.length; p++) { // create stats string statsRow = "<td>" + players[p][0] + "</td>"; statsRow += "<td>" + players[p][1] + "</td>"; statsRow += "<td>" + players[p][2] + "</td>"; statsRow += "<td>" + players[p][3] + "</td>"; statsRow += "<td>" + players[p][4] + "</td>"; // add row to table newRow = tableBody.insertRow(p); newRow.innerHTML = statsRow; }