Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJames Roberts
16,976 PointsButton removal
The final function towards the end of the video has me somewhat confused: btn.addEventListener('click', (event) => { getJSON(astrosUrl, (json) => { json.people.map(person =>{ getJSON(wikiUrl + person.name, generateHTML); }); }); event.target.remove(); });
How is event.target.remove() removing the button?
1 Answer
James Roberts
16,976 PointsThanks for breaking it down Phil. Its targeting the element targeted by the click. Got it. Thanks.
Phil Wood
4,379 PointsNo problem James, glad I could help.
Phil Wood
4,379 PointsPhil Wood
4,379 PointsHi James, You set a listener on the btn. You told the listener to react when the btn is clicked.
btn.addEventListener('click');
The 'event' is an object created when the listener detects the click.
(event)=>
The 'event' object contains a property called 'target', which is a reference to the btn (The object which caused the event).
event.target
The .remove() method is called on the 'event.target' - effectively removing the btn which caused the event.