|
| 1 | +<!-- .slide: id="lesson9" --> |
| 2 | + |
| 3 | +# JavaScript Course - Fall 2025 |
| 4 | + |
| 5 | +Lesson 9, Tuesday, 2025-10-14 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +### Recap: Objects |
| 10 | + |
| 11 | +```js |
| 12 | +let me = { |
| 13 | + name: "Ada", |
| 14 | + address: { |
| 15 | + country: "Germany", |
| 16 | + city: "Berlin" |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +console.log(me.name) |
| 21 | +console.log(me.address.city) |
| 22 | + |
| 23 | +me.name = "Annie Easley" |
| 24 | + |
| 25 | +console.log(me.name) |
| 26 | +``` |
| 27 | + |
| 28 | +* What's the result? |
| 29 | + |
| 30 | +```js |
| 31 | +Ada |
| 32 | +Berlin |
| 33 | +Annie Easley |
| 34 | +``` |
| 35 | +<!-- .element: class="fragment" --> |
| 36 | + |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +### "this" in JavaScript |
| 41 | + |
| 42 | +In JavaScript, `this` is a special keyword that refers to the object that the current function is a method of. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### Example |
| 47 | + |
| 48 | +```js |
| 49 | +let person = { |
| 50 | + name: "Grace", |
| 51 | + greet: function() { |
| 52 | + console.log("Hello, my name is " + this.name); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +person.greet(); |
| 57 | +``` |
| 58 | + |
| 59 | +* What's the result? |
| 60 | + |
| 61 | +```js |
| 62 | +Hello, my name is Grace |
| 63 | +``` |
| 64 | +<!-- .element: class="fragment" --> |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +### Recap: onclick Event |
| 69 | + |
| 70 | +The `onclick` event is a specific type that happens when a user clicks on an element, like a button, link, or image. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### Example |
| 75 | + |
| 76 | +```html |
| 77 | +<button onclick="sayHello();">Click Me</button> |
| 78 | +``` |
| 79 | + |
| 80 | +In this example, When a user clicks the button, it triggers the `sayHello` function |
| 81 | + |
| 82 | +```js |
| 83 | +function sayHello() { |
| 84 | + console.log('Hello, World!'); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +### Another event: oninput |
| 91 | + |
| 92 | +There are many more events similar to `onclick`: |
| 93 | + |
| 94 | +[MDN documentation - Mouse events](https://developer.mozilla.org/en-US/docs/Web/API/Element#mouse_events) |
| 95 | + |
| 96 | +We can use the `oninput` event to run a function every time the user inputs something in an input field. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### Example |
| 101 | + |
| 102 | +```html |
| 103 | +<input type="number" oninput="inputChanged();"> |
| 104 | +<script> |
| 105 | + function inputChanged() { |
| 106 | + console.log("yay, something changed"); |
| 107 | + } |
| 108 | +</script> |
| 109 | +``` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +### HTML and Javascript |
| 114 | + |
| 115 | +**HTML (Structure)**: HTML provides the basic structure of a webpage. It defines elements like headings, paragraphs, images, and more. It's like the skeleton of a webpage. |
| 116 | + |
| 117 | +```html |
| 118 | +<!DOCTYPE html> |
| 119 | +<html> |
| 120 | +<head> |
| 121 | + <title>Simple Page</title> |
| 122 | +</head> |
| 123 | +<body> |
| 124 | + <h1 id="greeting">Hello, World!</h1> |
| 125 | +<body> |
| 126 | +</html> |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +### Manipulate HTML Elements from JavaScript |
| 132 | + |
| 133 | +Let's say we have a HTML element: |
| 134 | + |
| 135 | +```html |
| 136 | +<div>Total Price: 0 EUR</div> |
| 137 | +``` |
| 138 | + |
| 139 | +Wouldn't it be nice if we could dynamically change that from JavaScript? |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +<!-- .slide: id="get-element" --> |
| 144 | + |
| 145 | +### document.getElementById |
| 146 | + |
| 147 | +One way to obtain a variable pointing to a HTML element is `document.getElementById()`: |
| 148 | + |
| 149 | +HTML: |
| 150 | + |
| 151 | +```html |
| 152 | +<div id="totalPrice">Total Price: 0 EUR</div> |
| 153 | +``` |
| 154 | + |
| 155 | +JavaScript |
| 156 | + |
| 157 | +```js |
| 158 | +let priceDiv = document.getElementById("totalPrice"); |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +`priceDiv` is a variable, and its type is `object`. |
| 164 | + |
| 165 | +How do we know which properties/methods the object has? |
| 166 | + |
| 167 | +We could try the browser's developer tools. |
| 168 | + |
| 169 | +Or we could check [MDN - HTML div element](https://developer.mozilla.org/en-US/docs/Web/API/HTMLDivElement) |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +Let's try some properties, see what happens: |
| 174 | + |
| 175 | +```js |
| 176 | +let priceDiv = document.getElementById("totalPrice"); |
| 177 | +// choose from below: |
| 178 | +priceDiv.textContent = "Hi from JavaScript"; |
| 179 | +priceDiv.hidden = true; |
| 180 | +priceDiv.style.backgroundColor = "red"; |
| 181 | +priceDiv.remove(); |
| 182 | +``` |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +### Example |
| 187 | + |
| 188 | +```html |
| 189 | +<!DOCTYPE html> |
| 190 | +<html> |
| 191 | +<body> |
| 192 | + <h1 id="greeting">Hello, World!</h1> |
| 193 | + <button onclick="changeText();">Click Me</button> |
| 194 | + |
| 195 | + <script> |
| 196 | + function changeText() { |
| 197 | + let greetingElement = document.getElementById("greeting"); |
| 198 | + greetingElement.textContent = "Hi, there!"; |
| 199 | + } |
| 200 | + </script> |
| 201 | +</body> |
| 202 | +</html> |
| 203 | + |
| 204 | +``` |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +### How to access the HTML via Javascript (or the DOM) |
| 209 | + |
| 210 | +* Add `id` to your HTML element |
| 211 | +* Create a variable for the element using `document.getElementById()` |
| 212 | +* Read or change any properties of the element using the variable and dot (`.`) notation |
| 213 | + |
| 214 | +--- |
| 215 | + |
| 216 | +### Task 1 |
| 217 | + |
| 218 | +In HTML, create a `div` element with width and height set to 100, and a `button`. |
| 219 | + |
| 220 | +When the user clicks the button, set the background color of the `div` to red. |
| 221 | + |
| 222 | +--- |
| 223 | + |
| 224 | +### Task 2 |
| 225 | + |
| 226 | +In HTML, create a `button` and a `div` element. |
| 227 | + |
| 228 | +When the user clicks the button, update the `textContent` of the `div` element with the amount of times the user has clicked the button. |
| 229 | + |
| 230 | +Example: "You clicked 12 times" |
| 231 | + |
| 232 | +--- |
| 233 | + |
| 234 | +### Task 3 |
| 235 | + |
| 236 | +We're creating a web shop selling pizza (or lasagne, or domoda)! |
| 237 | + |
| 238 | +Create a number input field in HTML that lets the user choose the amount of pizza: |
| 239 | + |
| 240 | +```html |
| 241 | +Choose the amount of pizza slices: |
| 242 | +<input type="number" id="amount" min="0" value="0" oninput="amountChanged();"> |
| 243 | +``` |
| 244 | + |
| 245 | +Use the `valueAsNumber` property of the number input to get the amount that the user selected in your `amountChanged` function. Output the total price the user has to pay to a `div` element. |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +### Bonus: Task 4 |
| 250 | + |
| 251 | +Extend your webshop to sell two products (e.g. pizza _and_ lasagne). |
| 252 | + |
| 253 | +Every product has a different price. Update the total price in the `div` element every time the user changes the amount of each product. |
| 254 | + |
| 255 | +--- |
| 256 | + |
| 257 | +### Bonus: Task 5 |
| 258 | + |
| 259 | +There's a special sale - if the user buys products which sums for more than 20 EUR, they get 10% discount. Show the discounted total price. |
| 260 | + |
| 261 | +--- |
| 262 | + |
| 263 | +### Solution Task 1 |
| 264 | + |
| 265 | +HTML: |
| 266 | + |
| 267 | +```html |
| 268 | +<button onclick="changeColor()">Click me!</button> |
| 269 | +<div id="myDiv" width="100" height="100">Hello</div> |
| 270 | + |
| 271 | +<script> |
| 272 | + function changeColor() { |
| 273 | + let myDiv = document.getElementById("myDiv"); |
| 274 | + myDiv.style.backgroundColor = "red"; |
| 275 | + } |
| 276 | +</script> |
| 277 | +``` |
| 278 | + |
| 279 | +--- |
| 280 | + |
| 281 | +### Solution Task 2 |
| 282 | + |
| 283 | +```html |
| 284 | +<button onclick="count()">Click me!</button> |
| 285 | +<div id="myDiv">You clicked 0 times</div> |
| 286 | + |
| 287 | +<script> |
| 288 | + let clickCount = 0; |
| 289 | +
|
| 290 | + function count() { |
| 291 | + clickCount += 1; |
| 292 | + let myDiv = document.getElementById("myDiv"); |
| 293 | + myDiv.textContent = "You clicked " + clickCount + " times"; |
| 294 | + } |
| 295 | +</script> |
| 296 | +``` |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +### Solution Task 3 |
| 301 | + |
| 302 | +```html |
| 303 | +Number of hummus: |
| 304 | +<input type="number" id="amount" min="0" value="0" oninput="amountChanged();"> |
| 305 | +<div id="priceDiv">0 EUR</div> |
| 306 | + |
| 307 | +<script> |
| 308 | + let hummusPrice = 5; |
| 309 | +
|
| 310 | + function amountChanged() { |
| 311 | + let amountInput = document.getElementById("amount"); |
| 312 | + let priceDiv = document.getElementById("priceDiv"); |
| 313 | + let amount = amountInput.valueAsNumber; |
| 314 | + let totalPrice = hummusPrice * amount; |
| 315 | +
|
| 316 | + priceDiv.textContent = totalPrice + " EUR" |
| 317 | + } |
| 318 | +</script> |
| 319 | +``` |
0 commit comments