This is an old revision of the document!
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.
src
to 'team.js', then create the file 'team.js'.team.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>
style.css
td, th { border-width: 1px; border-style: solid; } td { padding: 10px; } table { border-collapse: collapse; } #carlos { font-style: italic; } .age { background-color: yellow; } #carlos .age { font-weight: 900; } td.age { font-size: 150%; }
team.js
window.onload = function () { }
window.onload = function () { // generate player data player = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero']; }
// create row text statsRow = "<tr>"; statsRow += "<td>" + player[0] + "</td>"; statsRow += "<td>" + player[1] + "</td>"; statsRow += "<td>" + player[2] + "</td>"; statsRow += "<td>" + player[3] + "</td>"; statsRow += "<td>" + player[4] + "</td>"; statsRow += "</tr>";
statsRow
.player[]
references the data in the list called 'player'.// add row to table tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(0); newRow.innerHTML = statsRow;
<tbody>
being assigned to the variable tableBody
.<tbody>
tag) of our tableBody element and assign it to a variable called newRow
.newRow
with the text string we created earlier.window.onload = function () { // generate data player = ['David', 'Ospina Ramirez', 'Colombia', 33, 'Portero']; // create row text statsRow = "<tr>"; statsRow += "<td>" + player[0] + "</td>"; statsRow += "<td>" + player[1] + "</td>"; statsRow += "<td>" + player[2] + "</td>"; statsRow += "<td>" + player[3] + "</td>"; statsRow += "<td>" + player[4] + "</td>"; statsRow += "</tr>"; // 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++) { }
()
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; }