This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
en:web_development:lists:javascript [2021/12/02 13:42] mag |
en:web_development:lists:javascript [2023/08/16 09:33] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Web Development Lesson | + | ====== Web Development Lesson |
| ===== Javascript Lists ===== | ===== Javascript Lists ===== | ||
| + | ==== Objectives ==== | ||
| + | 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. | ||
| ==== Setup ==== | ==== Setup ==== | ||
| - | * We'll use the first file you created in this lesson called ' | + | * We'll use the first file you created in this lesson called '[[https:// |
| * Create a file called ' | * Create a file called ' | ||
| * We'll also return to [[https:// | * We'll also return to [[https:// | ||
| Line 38: | Line 40: | ||
| < | < | ||
| * Run the code and see that it displays ' | * Run the code and see that it displays ' | ||
| - | * Try changing | + | * Try changing |
| * Using this technique, we can display the results of our list in our Visual Studio page. | * Using this technique, we can display the results of our list in our Visual Studio page. | ||
| * Open ' | * Open ' | ||
| - | < | + | < |
| < | < | ||
| </ | </ | ||
| Line 101: | Line 103: | ||
| ==== Iterating Arrays ==== | ==== Iterating Arrays ==== | ||
| * So now that you understand how for loops work, we can apply it to an array. | * So now that you understand how for loops work, we can apply it to an array. | ||
| + | * Open your jsfiddle tab again. | ||
| * Let's edit the for loop you have already. | * Let's edit the for loop you have already. | ||
| < | < | ||
| for (i = 0; i < 10; i++) { | for (i = 0; i < 10; i++) { | ||
| - | text += i + "< | + | text += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = text;</ | document.body.innerHTML = text;</ | ||
| Line 110: | Line 113: | ||
| < | < | ||
| for (i = 0; i < 10; i++) { | for (i = 0; i < 10; i++) { | ||
| - | carList += i + "< | + | carList += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = carList;</ | document.body.innerHTML = carList;</ | ||
| Line 116: | Line 119: | ||
| < | < | ||
| for (i = 0; i < 3; i++) { | for (i = 0; i < 3; i++) { | ||
| - | carList += i + "< | + | carList += cars[i] + "< |
| } | } | ||
| document.body.innerHTML = carList;</ | document.body.innerHTML = carList;</ | ||
| - | * If you runt his now, it should show all 3 cars, one on each line. | + | * If you run his now, it should show all 3 cars, one on each line. |
| * Now have a go at applying this yourself to the list of players in Visual Studio Code. | * Now have a go at applying this yourself to the list of players in Visual Studio Code. | ||
| * Here's my version. | * Here's my version. | ||
| Line 128: | Line 131: | ||
| window.onload = function () { | window.onload = function () { | ||
| + | playerList = ""; | ||
| for (i = 0; i < 4; i++) { | for (i = 0; i < 4; i++) { | ||
| playerList += "< | playerList += "< | ||
| Line 139: | Line 143: | ||
| * Let's see how this works in jsfiddle. | * Let's see how this works in jsfiddle. | ||
| * Add an alert to your Javascript code. | * Add an alert to your Javascript code. | ||
| - | < | + | < |
| * Run that and check that it does what you think it should. | * Run that and check that it does what you think it should. | ||
| * So we can change our continuing condition for the list of players as follows. | * So we can change our continuing condition for the list of players as follows. | ||
| Line 145: | Line 149: | ||
| * Run it now and see that it shows exactly as many players as you have in your list. | * Run it now and see that it shows exactly as many players as you have in your list. | ||
| * Congratulations on creating a list using Javascript. | * Congratulations on creating a list using Javascript. | ||
| + | |||
| + | ==== Conditional Statements ==== | ||
| + | * Sometimes the action you take will depend on some other factor - user input, previous actions or information from a database for example. | ||
| + | * The simplest of these is the 'if statement' | ||
| + | * Let's jump back to jsfiddle to see how this works. Delete all the Javascript you have there at the moment. | ||
| + | * Enter the following code into the Javascript panel. | ||
| + | < | ||
| + | if (x == 2) { | ||
| + | alert 'the condition is true'; | ||
| + | }</ | ||
| + | * The basic structure of this code is < | ||
| + | * We have a condition inside the < | ||
| + | * In this case, our condition is < | ||
| + | * Since we've just defined < | ||
| + | * Now change the value of < | ||
| + | * Nothing is displayed. Let's add a message if it fails too. | ||
| + | < | ||
| + | if (x == 2) { | ||
| + | alert 'the condition is true'; | ||
| + | } else { | ||
| + | alert 'the condition is false'; | ||
| + | }</ | ||
| + | * Here, we have added < | ||
| + | * Now, let's apply that to our checklist in ' | ||
| + | * If the user checks a checkbox, we will make the associated text grey. If they uncheck it, we'll make the text black again. | ||
| + | * Add an attribute to each of the list items to call a Javascript function. | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | * < | ||
| + | * We will call our Javascript function ' | ||
| + | * Now, save ' | ||
| + | * Add our function below the existing code. | ||
| + | < | ||
| + | }</ | ||
| + | * First we define the code as a ' | ||
| + | * Next, we name the function ' | ||
| + | * Then we assign the information passed by the < | ||
| + | * Finally, we have < | ||
| + | * To make things a bit easier, we'll find the actual checkbox using the name, and assign that to a variable. Copy the following code inside the < | ||
| + | < | ||
| + | * We've called our checkbox ' | ||
| + | * This time we're finding our element by name instead of tag so we use < | ||
| + | * Now we can add our condition. | ||
| + | < | ||
| + | } else { | ||
| + | }</ | ||
| + | * If our checkbox ' | ||
| + | * The text isn't part of our checkbox, so we need to find the element it belongs to, which in this case is the parent element (our < | ||
| + | * The full code is then as follows. | ||
| + | < | ||
| + | cb = document.getElementsByName(item)[0]; | ||
| + | if (cb.checked) { | ||
| + | cb.parentElement.style.color = " | ||
| + | } else { | ||
| + | cb.parentElement.style.color = " | ||
| + | } | ||
| + | }</ | ||
| + | * Run that and try it out for yourself. | ||
| + | |||
| + | ==== Final Code ==== | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| [[en: | [[en: | ||