← The Trade Desk Interview Insights
Start by writing the JavaScript code that selects the button using document.querySelector and attaches a click listener with addEventListener. Then, explain that e.target refers to the element that triggered the event, which in this case is the button itself, and demonstrate how to access its text content. Keep the explanation concise and focus on the practical use of e.target for event delegation and dynamic elements.
Pro tip: Mention that using e.target allows the handler to work even if the button's content changes or if the listener is attached to a parent element for event delegation. This shows awareness of scalable event handling patterns.
Use document.querySelector to select the button element, e.g., const button = document.querySelector('button');. Ensure the selector is specific enough to target the intended button.
Call button.addEventListener('click', function(e) { ... }); to listen for click events. The handler receives the event object e.
Inside the handler, use console.log(e.target.textContent); to log the button's text. Alternatively, use this.textContent if using a regular function, but e.target is more explicit.
Clarify that e.target is the element that dispatched the event, i.e., the button that was clicked. It allows access to properties like textContent, id, or classList.
Mention that if the listener were attached to a parent, e.target could be a child element, enabling event delegation. This is useful for dynamic content.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.