I wasn’t sure about the difference between .onclick and .addEventListener, so I did some googling. A significant difference between the two seems to be whether we can set multiple event handlers.
As far as for this exercise, we can do almost the same thing using .onclick:
readMore.onclick = showInfo;
but when we want to set up multiple event handlers, a difference occurs.
For example, let’s consider to hide button when showing more info:
Using the .addEventListener() method, we can have a DOM element listen for a specific event and execute a block of code when the event is detected. The DOM element that listens for an event is called the event target and the block of code that runs when the event happens is called the event handler .
Question: Is it technically correct for the lesson to state that the element(the event target) listens for a specific event?
I would have said that the .addEventListener methodon the element listens, and that the second argument for this method is the event handler that runs in response to an occurring event.
Hi all. My code passes in this lesson, but doesn’t seem to do what the instructions imply it should. The idea seems to be that the code is triggered by clicking the button. It isn’t. It executes when I click ‘run’ in the editor window. This is a bit unclear. What’s the point in adding an event handler to a button when clicking the button doesn’t trigger the event, as it is apparently triggered when saving the code in the editor? My code follows:
let readMore = document.getElementById('read-more')
let moreInfo = document.getElementById('more-info')
// Write your code here:
const showInfo = () => {
moreInfo.style.display = 'block';
}
readMore.addEventListener('click', showInfo());
In the arguments for .addEventListener, showInfo() should be showInfo
meaning the showInfo function is being used as an argument (so that the function can be called later - when a click happens);
the showInfo function should not be called in that arguments for .addEventListener.