“When someone clicks on this item, run the toggleDone function and give it information about the click.”
Notice that we’re passing toggleDone, without parentheses.
toggleDone means “here is the function to run later.” toggleDone() means “run the function right now.”
We want the first one because we’re waiting for a click.
2. Where does e get its value?
Look at your function:
function toggleDone(e) {
if (!e.target.className) {
e.target.className = "done";
} else {
e.target.className = "";
}
}
You haven’t assigned a value to e yourself. Instead, the browser supplies an event object automatically when it calls your function.
Here’s what happens when you click a list item:
You click a list item.
The browser detects the click.
The browser calls toggleDone(eventObject).
The parameter e receives that event object.
Your function runs using the information in e.
Conceptually, it works like this:
function toggleDone(e) {
// e contains information about the click
}
// When a click occurs, the browser
// effectively does something like this:
toggleDone(eventObject);
eventObject is just a descriptive name here. You don’t need to create it yourself.
I have a follow up question:
Where could’ve I get the information about the last part?
“When someone clicks on this item, run the toggleDone function and give it information about the click.”
I’m sure it’s there somewhere but I just didn’t see it. If what you wrote would’ve been in the docs it would’ve been so much clearer to me. I guess I was expecting there to be something like that or what you wrote later:
Instead, the browser supplies an event object automatically when it calls your function.
I’m trying to become better at navigating and understanding the docs. That’s why I’m asking this.
That’s a great question! I think the information you’re looking for is in MDN’s Introduction to events guide, under the section titled “Event objects.”
That section explicitly explains that the browser automatically passes an event object to an event handler, and it demonstrates this with code very similar to yours:
function bgChange(e) {
e.target.style.backgroundColor = "red";
}
btn.addEventListener("click", bgChange);
The key sentence in that section is that the event object is automatically passed to event handlers to provide additional information and functionality.
You can also find the more technical explanation in the addEventListener() reference, under “The event listener callback.” It explains that the callback receives an Event object describing the event that occurred.
I agree that this can be confusing when you’re starting out. The addEventListener() syntax tells you which function to register, but understanding what arguments that function receives requires knowing how event listeners work.
One useful distinction is that e isn’t a special JavaScript keyword. It’s simply the parameter name you chose for your function. The browser passes the event object to the function, and JavaScript makes that argument available through the first parameter, e. You could just as easily write function toggleDone(event) and use event.target instead.
When navigating documentation, I’d recommend looking at both the method’s reference page and the broader guide explaining the concept. The reference tells you how to use a method, while the guide often explains the underlying behavior and shows it in context.
Hope that helps, and keep asking questions like these! Understanding why the code works is just as important as learning the syntax.
Thanks a ton @AC1DST4R for this reply! Just the kind of information I was looking for - very useful and practical!
I had a hunch that this topic will be covered in more detail later in the curriculum but I thought I would ask about it anyway. I’m very pleased to know that it will indeed be covered in the later parts!
The MDN documentation for addEventListener() and the general guide on events are indeed the best places to find this information. Specifically, the “Event objects” section in the events guide and the “The event listener callback” part of the addEventListener() reference explain that the browser automatically passes an Event object to the handler function. This object contains details about the event that occurred, which is then accessible via the parameter you define (like e or event).