async and await)async and awaitInstead of using then() method
async function getData() {
const url = "colleges.json"
const response = await fetch(url);
console.log(`Received response: ${response.status}`);
}
async and
awaitasync function getData() {
const url = "colleges.json"
const response = await fetch(url);
console.log(`Received response: ${response.status}`);
}
async marks that it is an asynchronous function
async and
awaitasync function getData() {
const url = "colleges.json"
const response = await fetch(url);
console.log(`Received response: ${response.status}`);
}
await will cause the function to ‘freeze’ till the
Promise is resolved
async and
awaitconst url = "colleges.json"
const fetchPromise = fetch(url);
fetchPromise
.then((response) => response.json())
.then((data) => {
console.log(data[0].name);
});
==
const getData = async () => {
const url = "colleges.json"
const response = await fetch(url);
const data = await response.json()
console.log(data[0].name)
}
async and
awaitconst getJson = async (url) => {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Error! ${response}`)
}
const data = await response.json()
console.log(data[0].name)
return data;
}
// Calling it from another `async` function
try {
let colleges = await getJson("colleges.json");
} catch(error) {
console.error(error);
}
async and await)createElement()const newDiv = document.createElement("div");
const p = document.createElement("p");
String argument specifying the types of element
createTextNode()const newText = document.createTextNode("HCDD 340");
String argument for the text
setAttribute()
const p = document.createElement("p");
// set id
p.setAttribute("id", "new-id");
// setting css class
p.setAttribute("class", "large-text");
Two arguments: name and value
class
attributeconst div = document.createElement("div");
div.className = "foo";
// <div class="foo"></div>
// add 3 classes
div.classList.add("foo", "bar", "baz");
// removes two classes
div.classList.remove("foo", "bar");
// if visible is set remove it, otherwise add it
div.classList.toggle("visible");
appendChild()const sect = document.querySelector("section");
const para = document.createElement("p");
// add at the end of the section
sect.appendChild(para);
Appends a node to the parent
insertBefore()<div id="parent">
<span id="child">foo</span>
</div>
// Create a new, plain <span> element
const sp1 = document.createElement("span");
// Get the reference element
const sp2 = document.getElementById("child");
// Get the parent element
const parentDiv = sp2.parentNode;
// Insert the new element into before sp2
parentDiv.insertBefore(sp1, sp2);
The new node will be placed before a reference node
after()let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.after(span);
// "<div><p></p><span></span></div>"
Insert the node after a given node
after() for text
nodelet container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
p.after("Text");
// "<div><p></p>Text</div>"
“Strings are inserted as equivalent Text nodes”
removeChild()<div id="parent">
<div id="child"></div>
</div>
const parent = document.getElementById("parent");
const child = document.getElementById("child");
const throwawayNode = parent.removeChild(child);
Removes the child node from the parent
const node = document.getElementById("child");
node.parentNode.removeChild(node);
parentNode points to the parent
main.js
output.html for the final outcomeOverview
sectionaddOverviewContent in main.jsH2addHeaderContent in main.jsaddObjectiveContent in main.jsH2list<input type="text" name="module" id="module">
<button id="btn_add">Add a module!</button>
const addModule = (event) => {
const input = document.querySelector('input');
const moduleName = input.value;
};
target.addEventListener("keydown", (event) => {
/* Key pressed */
});
target.addEventListener("keyup", (event) => {
/* Key released */
});
event
object contains key informationkey: value of the key pressedctrlKey: was Ctrl key active?shiftKey: was Shift key active?metaKey: was the meta key active?
window.addEventListener("keydown", event => {
if (event.key == "v") {
document.body.style.background = "violet";
}
});
window.addEventListener("keyup", event => {
if (event.key == "v") {
document.body.style.background = "";
}
});
“Page turns violet when you hold the V key”
window.addEventListener("keydown", event => {
if (event.key == "v") {
document.body.style.background = "violet";
}
});
window.addEventListener("keyup", event => {
if (event.key == "v") {
document.body.style.background = "";
}
});
window contains
the DOM document
window makes items globally available (functions, event
handlers, objects)