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/01 16:10] 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:// | ||
* We'll start with a blank slate for this, so if you have anything in the HTML, CSS or Javascript panels of jsfiddle, delete them. | * We'll start with a blank slate for this, so if you have anything in the HTML, CSS or Javascript panels of jsfiddle, delete them. | ||
+ | |||
+ | ==== External Scripts ==== | ||
+ | * We'll start in Visual Studio Code. | ||
+ | * Just as we created an entry in < | ||
+ | * Create a new line in < | ||
+ | * Type the name of our javascript file ' | ||
+ | < | ||
+ | * We can now work in our javascript file and the browser will find it when we open the ' | ||
==== Arrays ==== | ==== Arrays ==== | ||
- | | + | * An ' |
- | | + | * Type the following text into ' |
- | < | + | < |
- | * By now you should be familiar with the form of the statement. < | + | 'Johan Andres Mojica Palacio', |
- | * < | + | ' |
+ | 'Duvan Esteban Zapata Banguera' | ||
+ | * By now you should be familiar with the form of the statement. < | ||
+ | * < | ||
* Each of the player names is text, so we surround them with < | * Each of the player names is text, so we surround them with < | ||
- | * I've included names of 3 cars, but there are many more. Feel free to add any others you like using the same format. | + | * I've included names of 4 Colombian football players, but there are many more. Feel free to add any others you know using the same format. |
- | * To be able to display these items, we need to be able to reference them. | + | * I've written each of the names on a separate line to make it clear. This is good practice, but it's not essential - you can string them all on the same line as long as you separate the items with a < |
- | * You've seen how to do this before. In the previous lesson, we accessed elements with specific tags using < | + | |
- | * We now need to take each of the names in this array and print them on the screen | + | ==== Referencing Array Elements ==== |
+ | * To display these items, we need to be able to reference them. | ||
+ | * You've seen how to do this before. In the previous lesson, we accessed elements with specific tags using < | ||
+ | * We can see this more easily in jsfiddle. | ||
+ | * Create a simple | ||
+ | < | ||
+ | * Here, the array is simple enough that I've left it on a single line. | ||
+ | * Now, let's display the first element in the list on our page. | ||
+ | < | ||
+ | * Run the code and see that it displays ' | ||
+ | * Try changing the number inside the < | ||
+ | * Using this technique, | ||
+ | * Open ' | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | * We'll use this to display our Javascript array. | ||
+ | * After the players list in ' | ||
+ | < | ||
+ | playerList += "< | ||
+ | playerList += "< | ||
+ | playerList += "< | ||
+ | playerList += "< | ||
+ | * Can you see how this works? We're building | ||
+ | * Next we need to display the string we've created. | ||
+ | < | ||
+ | * This looks for the 3rd < | ||
+ | * If you run the code now, it probably won't work because the Javascript code executes | ||
+ | < | ||
+ | document.getElementsByTagName(' | ||
+ | }</ | ||
+ | * < | ||
+ | * < | ||
+ | * When this is true, the browser will run the < | ||
+ | * You don't need to remember or understand all this completely, but you should be able to recognise this pattern and be able to apply it when necessary. We'll use it often during the course so you become familiar with it. | ||
+ | * The code should work now when you run it, but if you've added all 16 players, listing them all would take more work. And some lists (like the list of products on MercadoLibre) have thousands of entries. | ||
+ | * We can save ourselves a lot of time by writing | ||
==== For Loops ==== | ==== For Loops ==== | ||
+ | * The name ‘for loop’ will seem strange if you’re new to programming. It essentially means that ‘for’ each item in the list, we will ‘loop’ through a given block of code. ie. we’re going to apply the same code (add the item to our string) to every player. | ||
+ | * Let’s go back to jsfiddle to see how this works. | ||
* Type the following code into the Javascript section below the cars array. | * Type the following code into the Javascript section below the cars array. | ||
< | < | ||
Line 27: | Line 78: | ||
* For each of these, by convention, we'll use a variable < | * For each of these, by convention, we'll use a variable < | ||
* < | * < | ||
- | * We'll run the code as long as < | + | * We'll run the code as long as < |
* Finally, < | * Finally, < | ||
* Let's see this in action by adding an ' | * Let's see this in action by adding an ' | ||
Line 52: | 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 61: | 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;</ | ||
- | * In the for loop, the starting condition < | + | * In the for loop, the starting condition < |
- | * I only have 3 cars in my list, so I could write < | + | |
- | * You can discover the number of elements using < | + | |
< | < | ||
- | for (i = 0; i < cars.length; i++) { | + | for (i = 0; i < 3; i++) { |
- | carList += i + "< | + | carList += cars[i] + "< |
} | } | ||
document.body.innerHTML = carList;</ | document.body.innerHTML = carList;</ | ||
- | * Run it now and see that it shows exactly as many elements as you have in your list. | + | * If you run his now, it should |
- | * We can then change the display code to show the list item tag and the car name corresponding to the current value of < | + | * Now have a go at applying this yourself |
- | < | + | * Here's my version. |
- | for (i = 0; i < cars.length; i++) { | + | |
- | carList += cars[i] + "< | + | |
- | } | + | |
- | document.body.innerHTML = carList;</ | + | |
- | * Finally, we need to display the items as an HTML list. | + | |
- | * Update the string as follows. | + | |
- | < | + | |
- | * Notice that we're missing the type of list. Let's add an unordered list to the HTML document. | + | |
- | < | + | |
- | + | ||
- | </ | + | |
- | * Then we update the display statement to refer to our < | + | |
- | < | + | |
- | * Run your code to see the bulleted | + | |
- | + | ||
- | ==== External Scripts ==== | + | |
- | * Let's see how this all works on a real site using Visual Studio Code. | + | |
- | * Just as we created an entry in < | + | |
- | * Create a new line in < | + | |
- | * Type the name of our javascript file ' | + | |
- | < | + | |
- | * We can now work in our javascript file and the browser will find it when we open the ' | + | |
- | + | ||
- | ==== Arrays ==== | + | |
- | * An ' | + | |
- | * Type the following text into ' | + | |
< | < | ||
' | ' | ||
' | ' | ||
- | ' | + | ' |
- | * By now you should | + | |
- | * < | + | window.onload = function () { |
- | * Each of the player names is text, so we surround them with < | + | playerList = ""; |
- | * I've included names of 4 Colombian football players, but there are many more. Feel free to add any others you know using the same format. | + | for (i = 0; i < 4; i++) { |
- | * I've written each of the names on a separate line to make it clear. This is good practice, but it's not essential - you can string them all on the same line as long as you separate | + | playerList += "< |
- | * We now need to take the names in this array and print them on the screen | + | } |
+ | document.getElementsByTagName(' | ||
+ | }</ | ||
+ | * Did you have something similar? | ||
+ | * If you added more players, you won' | ||
+ | * In fact, if you pull a list from a database, which we'll do much later in the course, you likely won't know how many items are in the ilst. What do you do then? | ||
+ | * Thankfully, Javascript can count the number items in an array for you using < | ||
+ | * Let's see how this works in jsfiddle. | ||
+ | * Add an alert to your Javascript code. | ||
+ | < | ||
+ | * 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. | ||
+ | < | ||
+ | * 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. | ||
+ | |||
+ | ==== Conditional Statements ==== | ||
+ | * Sometimes | ||
+ | * 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. | ||
+ | <code>var x = 2; | ||
+ | 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 | ||
+ | < | ||
+ | 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 | ||
+ | * 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 | ||
+ | < | ||
+ | * 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: |