When the button is clicked, put Clicked into #out.
Acceptance criteria
- 1.#out should show Clicked.
JavaScript Tutorial · JavaScript Events
These 10 tasks go with the JavaScript Events chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A JavaScript event is something that happens on a page, and code can run in response.” .
1.Handle a clickexercise · easy · 10 min
When the button is clicked, put Clicked into #out.
Passes when: #out should show Clicked.
2.Count the clicksexercise · easy · 10 min · practice plan
Show how many times the button has been clicked.
Passes when: #out should show 3.
3.Use a named handlerexercise · easy · 10 min · practice plan
Define a named function and attach it as the click handler.
Passes when: #out should show Handled.
4.Handle two buttonsexercise · easy · 10 min · practice plan
Make each button report its own name when clicked.
Passes when: #out should show Save.
5.Read a value on clickexercise · medium · 10 min · practice plan
When the button is clicked, show the value of the input.
Passes when: #out should show Ada.
6.Fix the handler called immediatelybug fix · easy · 8 min
Brackets run the function instead of passing it to the listener.
Passes when: Clicking the button should make #out show Clicked.
7.Fix the on prefix in the event namebug fix · easy · 8 min · practice plan
addEventListener takes click, not onclick.
Passes when: Clicking the button should make #out show Clicked.
8.Fix the listener on the wrong elementbug fix · easy · 8 min · practice plan
The listener was attached to the output instead of the button.
Passes when: Clicking the button should make #out show Clicked.
9.Fix the counter reset each timebug fix · easy · 8 min · practice plan
Declaring the count inside the handler resets it on every click.
Passes when: Clicking three times should make #out show 3.
10.Fix the second onclick overwriting the firstbug fix · easy · 8 min · practice plan
The onclick property holds only one handler.
Passes when: Clicking should make #out show both ran.
Between them these tasks cover addEventListener, Handlers and click.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.