lec-js-06
March 13th: Midterm 2 (JS)
March 15th: Withdraw Deadline
April 5th: Project Submission
April 11th: Final Exam
◉ How can we react to form submissions?
◉ What is event propagation?
◉ What is event delegation?
◉ Why is event delegation useful?
We want to react to the submit event.
We want to add something to the DOM based on form input - but we DON'T want the form to make a request!
example
document.querySelector("form").addEventListener("submit", console.log);What's going on here?
How can we stop it?
function stopSubmitAndLog(submitEvent) {
console.log(submitEvent);
submitEvent.preventDefault();
}Tell the browser NOT to do its usual thing when a form is submitted!
This submit event - and as we'll see later, ALL events - have a very useful property: target.
The target property holds a reference to the element that first reacts to the event.
In this case, it's a reference to the form element.
function stopSubmitAndLogFieldValues(submitEvent) {
let form = submitEvent.target;
let name = form.name.value;
let coord = form.coord.value;
console.log("name:", name);
console.log("coord:", coord);
submitEvent.preventDefault();
}We can ask the form for one of its input elements using that element's NAME. And we can get that input's value using the value property.
The target of a submit event is the form that was submitted.
01-submit
It'll make more sense than just going over a definition.
02a-propagation
⚠️ BTW - we can have an element listen for more than one event. AND an element can run more than one handler on a given event, too!
02b-propagation
Events propagate up ("bubble") from the target of an event, through all ancestor elements, right up to the document element.
If any ancestor element is listening for an event, they will "hear" it - and react to it - even if that event didn't directly happen to them.
Say we want the div's background colour to change to blue when the div is clicked.
03-simple-delegation
While it's true we could attach an event handler to the div to handle this...could we accomplish the same thing by attaching an event handler to the main?
But that assumption would be wrong!
Let's look at 2 examples.
04a-no-delegation
In this example, we have many divs inside of a container. When someone clicks on a div, we want the background colour and font colour to change.
We could do this by attaching an event handler to every div we create.
🤔How many event handlers are we creating?
🤔Are event handlers "free"? Take a look at the Memory tab in our dev tools.
04a-yes-delegation
Let's try doing this using event delegation instead.
🤔Now many event handlers are we creating?
🤔How are things looking in the Memory tab now?
05a-no-delegation
In this example, let's assume that we have a variety of elements that we'd like to possibly add to our page that may or may not be deleted by the user.
Notice that if we want to make something removable, we need to add JS code.
We could do this by adding a handler to each type of element that is added to our page.
05a-no-delegation
🤔 If we want to make something removable now, what do we need to do?
Let's try it now with event delegation.
🤔 Do we need to write JS to make something removable using this method?