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.
Constraints & Assumptions
-
Preserve the scope, facts, inputs, and requested outputs from the prompt above.
-
If the prompt leaves a detail unspecified, state a reasonable assumption before relying on it.
-
Keep the answer interview-ready: concise enough to present, but concrete enough to implement or evaluate.
Clarifying Questions to Ask
-
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
-
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
-
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?