lec-js-05
March 13th: Midterm 2 (JS)
March 15th: Withdraw Deadline
April 5th: Project Submission
April 11th: Final Exam
◉ How can we effectively add multiple elements?
◉ How can we react to user actions on a web page? (multiple examples)
<div class="game-tile">
<h2 data-bgg-id="35677">Le Havre</h2>
<img src="./img/le-havre-small.jpg" alt="Le Havre" />
</div>What if we wanted to make a bunch of them?
Hint: do you know a way of turning an array of something into an array of something else? 🤔
01-element-factory
What kind of actions can a user take when they interact with a web page? 🤔
There are many (many many) types of events that can occur in a web page.
In this course, we'll only dabble with a few specific events, but that's ok: once you know how to deal with one event, you'll have a good idea how to deal with any event if the need arises in the future.
Write down the guiding sentence
"When a user [ some action ], [ some result ]."
This helps us figure out what specific event we want to react to.
This helps us focus on what needs to happen when the event fires - it tells us what our event handler needs to do.
Define an event handler
Register that handler
Fig 9.10 from the textbook
We want to react to the keydown event.
(Yes, you could use keyup or keypress successfully here. Feel free to investigate their differences.)
We want to toggle a nav element's visibility when the event fires.
When reacting to someone hitting a key, I suggest telling either the document or the body element to listen for the keydown event.
document.addEventListener("keydown", console.log);
document.querySelector("body").addEventListener("keydown", console.log);
document.body.addEventListener("keydown", console.log);⚠️Use only ONE of these! 😁
Notice any useful property?
You can use an anonymous function (standard or arrow form) if you'd like...but having a well-named function declaration helps a lot.
let nav = document.querySelector("nav");
function toggleNavVisibility(event) {
if (event.key === "h") {
nav.classList.toggle("hidden");
}
}
document.body.addEventListener("keydown", toggleNavVisibility);Put this here so it is just runs once.
Make sure we only deal with 'h'!
Callback time!
02-keydown
We want to react to the input event.
We want to change a button's state from disabled to enabled.
document.querySelector("input").addEventListener("input", console.log);Notice any useful properties here?
let inputElem = document.querySelector("input");
let buttonElem = document.querySelector("button");
function enableButtonIfAPressed(event) {
if (event.data === "a") {
buttonElem.disabled = false;
}
}
inputElem.addEventListener("input", enableButtonIfAPressed);03-input
03a-input
In this case, we can't use the event's data property. We need to talk to the input itself and ask it what text it has.
⚠️ Note that our event hander does NOT have to use the event! This is perfectly ok!
BTW, this variant is useful for the Project's movie title search requirements....
We want to react to the click event.
We want to add a new element to the DOM.
let addQuestionElem = document.querySelector("#add-question");
addQuestionElem.addEventListener("click", console.log);There isn't really anything here I need - I just want to know that the div was clicked!
So just like our last input variant, we don't need to use the event object in our handler.
04-click