diff --git a/index.html b/index.html index 7b42cba..18b9060 100644 --- a/index.html +++ b/index.html @@ -41,6 +41,7 @@ + diff --git a/lesson14.md b/lesson14.md new file mode 100644 index 0000000..fc4c6df --- /dev/null +++ b/lesson14.md @@ -0,0 +1,304 @@ + + +# Basic Frontend - Fall 2024 + +Lesson 14, Tuesday, 2024-11-12 + +--- + +### Lesson overview + +* Recap +* create new HTML Elements + +--- + +## Recap - Loops + +What loops did we learn about? + +--- + +```js +let i = 0; +while (i < 5) { + console.log(i); + i++; +} +``` + + +```js +for (let i = 0; i < 5; i++) { + console.log(i); +} +``` + + +--- + +### Arrays and loops + +```js +let fruits = ["apple", "banana", "cherry"]; +for (let i = 0; i < fruits.length; i++) { + console.log(fruits[i]); +} +``` + +--- + +### Task + +Create an array of objects that represent your friends. Each person should have a `name` and an `age` property. Then, loop over the array and print out the name and age of each person as follows: `Name: Alice, Age: 25` + +--- + +### Recap - get Element By Id + +HTML: +```html +
+``` + +How do we change the content of this div? + + +```js +let myDivElement = document.getElementById("myDiv"); +myDivElement.textContent = "Hello world!"; +``` + + +--- + +HTML: +```html + +``` + +What JavaScript code do I need to write to change the input element to: + +```html + +``` + +We can add HTML attributes like this: + + +```js +let myInputElement = document.getElementById("myInput"); +myInputElement.type = "number"; +``` + + +--- + +### Another example + +HTML: +```html + +``` + +JS: +```js +let myInputElement = document.getElementById("myInput"); +myInputElement.type = "number"; +myInputElement.min = 0; +myInputElement.max = 99; +myInputElement.placeholder = "Amount"; +``` + +--- + +### Setting onclick in javascript? + +HTML + +```html + +``` + +How can we set the `onclick` property from JavaScript? + +```js +let myButtonElement = document.getElementById("myButton"); +// ??? +``` + +--- + +### Variables pointing to functions + +Before moving forward, we need to know a new concept. + +So far, we learned that variables can point to values: + +```js +let age = 42; +let name = "Otto"; +let isHappy = true; +let address = { + street: "Ottostr.", + city: "Berlin" +}; +``` + +--- + +### Functions can be values, too + +Variables can also point to functions: + +```js +let myFunction = function() { + console.log("this function was called"); +}; +// myFunction is a variable pointing to our function +myFunction(); // calls the function above +``` + +The function above is also called an "anonymous" function, because the function +itself has no name. + +--- + +### Syntax comparison + +```js +function myFunction(name) { + console.log("Hi " + name); +} +let myFunction2 = function(name) { + console.log("Hi " + name); +} +myFunction("Linus"); // "Hi Linus" +myFunction2("Brendan"); // "Hi Brendan" +``` + +--- + +HTML + +```html + +``` + +So, knowing this, can you tell how can we set the `onclick` property from JavaScript? + +```js +let myButtonElement = document.getElementById("myButton"); +myButtonElement.onclick = function() { + console.log("My button was clicked"); +} +``` + + +--- + +So far, we can get elements from HTML and manipulate them. + +* We can set and get properties +* ...properties can be numbers, strings, booleans, object, functions +* But what if we want to create a new element that's not in HTML yet? + +--- + + + +### document.createElement + +Basic usage: +```js +let myDiv = document.createElement("div"); +``` + +However, we need to do a bit more configuration to have something useful. + +--- + + + +```js +let myDiv = document.createElement("div"); // 1 +myDiv.textContent = "hello"; // 2 +document.body.appendChild(myDiv); // 3 +//