This is an old revision of the document!
In this exercise you will use an array to store a list of items, use a for loop to repeat a block of code, and use an if statement to run different instructions based on user input.
<head> to link to the external style sheet, we need to tell the browser where to find our javascript code.<head> and type 'script', then select 'script:src'. src attribute.<script src="lists.js"></script>
players = ['David Ospina Ramirez',
'Johan Andres Mojica Palacio',
'Gustavo Leonardo Cuellar Gallego',
'Duvan Esteban Zapata Banguera'];
player is the name of a variable and it now holds everything after the = sign. [] identifies an array, in this case holding a list of players. ' signs. , .document.getElementsByTagName('span')[2]. document.getElementsByTagName('span') is an array (a list) of all elements with the tag span. [2] means that we access the 3rd item in the list. Remember that the first item is [0] .cars = ['Ferrari', 'Porsche', 'Lamborghini'];
document.body.innerHTML = cars[0];
[] and check the results to be sure you understand how it works.</body> tag.<h2>Players</h2> <ol></ol> </body>
playerList = ""; playerList += "<li>" + players[0] + "</li>"; playerList += "<li>" + players[1] + "</li>"; playerList += "<li>" + players[2] + "</li>"; playerList += "<li>" + players[3] + "</li>";
document.getElementsByTagName('ol')[2].innerHTML = playerList;
<ol> tag and inserts the list string there.<ol> tag doesn't exist when the code runs). To avoid this, we need to wrap the display command inside a function that only runs when the page is fully loaded.window.onload = function() {
document.getElementsByTagName('ol')[2].innerHTML = playerList;
}
window, like document identifies the whole page. .onload triggers when the HTML code has finished loading.function containing all the code between {} .for (i = 0; i < 10; i++) {
}
for is the function, which will run code inside the {} a number of times according to the rules inside () . ; . i , short for 'iterator'.i = 0 means that the first time we run the code, the variable i will have a value of zero.i < 10 is true.i++ is the same as i = i + 1 so each time we loop through the code, we'll increment the variable i . So it will run with a value of i incrementing from 0 to 9. i . {} .alert("i = " + i);
i . // . {} .text += i + "<br />"; document.body.innerHTML = text;
text is a variable we'll use to hold the text to be displayed. += means that we'll add the new text to the full string we're building. i and a <br> tag which adds a new line.document.body.innerHTML refers to the entire content (body) of the HTML document.text) to the HTML body.text is the first time it sees it.var text = "";
text is a variable (var of type String ( "" ).var text = "";
for (i = 0; i < 10; i++) {
text += cars[i] + "<br />";
}
document.body.innerHTML = text;
var carList = "";
for (i = 0; i < 10; i++) {
carList += cars[i] + "<br />";
}
document.body.innerHTML = carList;
i = 0 and iterating statement i++ are still valid, but we need to adjust the continuing condition to stop at 3 items.var carList = "";
for (i = 0; i < 3; i++) {
carList += cars[i] + "<br />";
}
document.body.innerHTML = carList;
players = ['David Ospina Ramirez',
'Johan Andres Mojica Palacio',
'Gustavo Leonardo Cuellar Gallego',
'Duvan Esteban Zapata Banguera'];
window.onload = function () {
playerList = "";
for (i = 0; i < 4; i++) {
playerList += "<li>" + players[i] + "</li>";
}
document.getElementsByTagName('ol')[2].innerHTML = playerList;
}
.length .alert("cars has " + cars.length + " items.");
for(i = 0; i < players.length; i++) {
var x = 2;
if (x == 2) {
alert 'the condition is true';
}
if (condition) { code to execute if true }. () , and only if that condition is true do we run the code inside {} . x == 2 , which checks the value of x and the value of 2 . If the value of x is 2, then it will run the code displaying an alert. x = 2 (first line), this condition is true and the alert will be displayed. Try it now. x to 5 (or anything but 2) and run the code again.var x = 2;
if (x == 2) {
alert 'the condition is true';
} else {
alert 'the condition is false';
}
else with code that will run if the condition is not met. Run the code again with the wrong value of x and see the new statement. <li><input type="checkbox" name="flour" onchange="toggleCheckbox('flour')">100g flour</li>
<li><input type="checkbox" name="eggs" onchange="toggleCheckbox('eggs')">2 eggs</li>
<li><input type="checkbox" name="milk" onchange="toggleCheckbox('milk')">300ml milk</li>
onchange="" will trigger whenever there is a change to the element - in this case when the checkbox is checked or unchecked. When triggered it will run the Javascript code between the "".function toggleCheckbox(item) {
}
function which means it will only run when specifically called. In this case we'll call it by clicking the checkbox, but it could also be called from other code.onChange attribute (the name of the checkbox) to the variable called item. {} ready to contain our code to be run. {} .cb = document.getElementsByName(item)[0];
.getElementsByName. Then we choose the first in the list ([0]). if (cb.checked) {
} else {
}
function toggleCheckbox (item) {
cb = document.getElementsByName(item)[0];
if (cb.checked) {
cb.parentElement.style.color = "gray";
} else {
cb.parentElement.style.color = "black";
}
}