Add a click listener to a button

Quick Overview

Add a click listener to a button evaluates requirements, assumptions, structured reasoning, trade-offs, and verification in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Add a click listener to a button

Company: The Trade Desk

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Technical Screen

Given an HTML page that includes a button with the id 'my-button', write JavaScript to add a click event listener so that when the button is clicked, the button's text content is logged to the console. Use document.querySelector and addEventListener. Briefly explain what e.target refers to in this context.

Quick Answer: Add a click listener to a button evaluates requirements, assumptions, structured reasoning, trade-offs, and verification in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

|Home/Software Engineering Fundamentals/The Trade Desk
The Trade Desk logo
The Trade Desk
Jul 26, 2025, 12:00 AM
easySoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
8
0

Add a click listener to a button

JavaScript: Add a Click Listener to Log Button Text

Context

Assume your page contains a button element like:

<button id="my-button">Click me</button>

Task

  • Use document.querySelector and addEventListener to add a click handler to the button with id "my-button".
  • When the button is clicked, log the button's text content to the console.
  • Briefly explain what e.target refers to in the click handler.

Clarifying Questions to Ask Guidance

  • Clarify the goal, inputs, constraints, stakeholders, and success criteria.
  • State assumptions before using them.
  • Keep the answer grounded in the prompt rather than adding outside facts.

What a Strong Answer Covers Guidance

  • A structured framing of the problem and constraints.
  • A concrete approach with trade-offs and edge cases.
  • A way to validate the answer and communicate the recommendation.

Follow-up Questions Guidance

  • What assumption is most important to validate first?
  • What could make the answer fail in practice?
  • How would you explain the result to a non-technical stakeholder?
Loading comments...