Lesson 14, Thursday, 2024-05-16
- Recap
- create new HTML Elements
What loops did we learn about?
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
let fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
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
HTML:
<div id="myDiv"></div>
How do we change the content of this div?
let myDivElement = document.getElementById("myDiv");
myDivElement.textContent = "Hello world!";
HTML:
<input id="myInput">
What JavaScript code do I need to write to change the input element to:
<input id="myInput" type="number">
let myInputElement = document.getElementById("myInput");
myInputElement.type = "number";
HTML:
<input id="myInput" type="number" min=0 max=99 placeholder="Amount">
JS:
let myInputElement = document.getElementById("myInput");
myInputElement.type = "number";
myInputElement.min = 0;
myInputElement.max = 99;
myInputElement.placeholder = "Amount";
HTML
<button id="myButton" onclick="myFunction()">Click me!</button>
How can we set the onclick
property from JavaScript?
let myButtonElement = document.getElementById("myButton");
// ???
So far, we learned that variables can point to values:
let age = 42;
let name = "Otto";
let isHappy = true;
let address = {
street: "Ottostr.",
city: "Berlin"
};
Variables can also point to functions:
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.
function myFunction(name) {
console.log("Hi " + name);
}
let myFunction2 = function(name) {
console.log("Hi " + name);
}
myFunction("Owen"); // "Hi Owen"
myFunction2("Harald"); // "Hi Harald"
HTML
<button id="myButton">Click me!</button>
How can we set the onclick
property from JavaScript?
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?
Basic usage:
let myDiv = document.createElement("div");
However, we need to do a bit more configuration to have something useful.
let myDiv = document.createElement("div"); // 1
myDiv.textContent = "hello"; // 2
document.body.appendChild(myDiv); // 3
// <div>hello</div> has been added to the page!
- Create a new HTML element using
document.createElement
. Pass the type of the element (div
,button
,img
, ...) as string. - Set all the properties you like, e.g.
textContent
,onclick
, ... appendChild
add an element to the end of the list of children of a specific parent element. Here, we append it to the document's<body>
.
What is the function appendChild
?
document.body.appendChild(myDiv);
Remember our <body>
tag in the html? document.body
refers to that element, it is mostly just like any other HTML element. Content within here is what the user sees in their browser
appendChild
means to "add within this element", and it is the best way to add new elements to the page. appendChild
can be called on almost any element.
Let's say you have this HTML:
<div id="myDiv"></div>
How do you turn it to this from JavaScript?
<div id="myDiv">
<span>Hello</span>
</div>
- Hint: Use
document.getElementById
,document.createElement
, thetextContent
property andappendChild
.
Use a for loop and createElement to generate the following HTML on your page:
<div>Element 1</div>
<div>Element 2</div>
<div>Element 3</div>
<div>Element 4</div>
<div>Element 5</div>
Create a button at the top of the page that generates a new element in sequence when clicked, so "Element 6", "Element 7", and so on.
It should have one <input>
field where the user can enter a shopping item. It should have one <button>
. When the user clicks the button, the text from the <input>
field should be appended to the shopping list.
When the user clicks on a shopping item, remove it from the page.
- Hint: set an
onclick
function on your newly created HTML element. - Hint: use the
remove()
method of your item to remove it.