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; }
function createRowText() { }
function createRowText() { 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>"; }
player
is and the main code won't have access to statsRow
.player
between ()
to tell the original code that we need this information.return statsRow;
to pass the string back to the main code. It should now look like this.function createRowText(player) { 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>"; return 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.// create text for (p = 0; p < players.length; p++) { }
i
with p
. We're running through the code from player 1 (p = 0
) until we reach the last player (p < players.length
) incrementing each time ( p++
) so we hit every player.console.log(p);
p
in the console, which is a tool that allows you to interact with the javascript code in the page. p
to players[p]
which should be the data for each player.// create row text statsRow = createRowText(players[p]);
statsRow
because it fits our code for adding to the table, but this is a different variable to the one inside the function. The one inside the function only works inside the function (its 'scope' is the function).createRowText(players[p])
calls the function createRowText
and provides it the data belonging to the current player as we saw in the Console.statsRow = createRowText(players[p])
reassigns statsRow
).for (p = 0; p < players.length; p++) { console.log(players[p]); // create row text statsRow = createRowText(players[p]); // add row to table tableBody = document.getElementsByTagName("tbody")[0]; newRow = tableBody.insertRow(0); newRow.innerHTML = statsRow; }
.insertRow[0]
).newRow = tableBody.insertRow(p);
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.window.onload = function () { // generate data 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]; // display data for (p = 0; p < players.length; p++) { // console.log(players[p]); // create row text statsRow = createRowText(players[p]); // add row to table newRow = tableBody.insertRow(p); newRow.innerHTML = statsRow; } } function createRowText(player) { 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>"; return statsRow; }