Day 15: JavaScript Challenge
LocalStorage
and event delegation are concepts I am familiar with. However,
I am very excited doing this challenge!
Have you ever imagined adding event listener to element that never exist or will only exist in the future? That’s impossible, right? However, our assumption sometime tells otherwise.
Let’s say we want to delete item from to-do list (which is dynamically created and destroyed) whenever we click ✖ button on it. If we attach event listener to ✖ button, you won’t get anything works correctly if the item was newly created because you cannot attach event listener to nonexistence DOM element. Perhaps, it functions correctly for existing elements because it’s already on the page, but we’ve been wrong conceptually. That’s why we need event delegation to fix that problem.
Event delegation is not that fancy! It’s like attaching event listener
to parent element (as for ul
, there must be a bunch of li
inside it), so
child elements can function specifically without having to attach any event
listeners to them.
Pay attention to this snippet:
Perhaps, my wording is not as cute as ちえさ せれな. I will let David Walsh explain it if you find my explanation confusing. Here’s his blog.
I finish the challenge this way. Have a look at the demo. I am off now!
I promise, I will explain this snippet incrementally but thoroughly.
HTML
JavaScript
Alright then, that’s it for today. I long for ちえさ せれな’s cute ponytail. I cannot wait any longer!