JavaScript preventDefault — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript preventDefault lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Stop a link navigating
easy10 minCancel the link and report that the page stayed.
- 2
Stop a form reloading
easy10 minCancel the submit and report that it was handled.
- 3
Check it was prevented
easy10 minReport the value of defaultPrevented after cancelling.
- 4
Stop propagation
easy10 minStop the click reaching the box and report handled here.
- 5
Filter by target instead
medium10 minHandle only the button, without stopping propagation.
Find the bug(5)
- 1
Fix preventDefault called without brackets
easy8 minIt is a method and must be called.
- 2
Fix stopPropagation used to cancel an action
easy8 minStopping the travel does not stop the browser navigating.
- 3
Fix preventDefault used to stop the bubble
easy8 minThe click should not reach the box, which needs stopPropagation.
- 4
Fix return false used to cancel
easy8 minReturning false does nothing in an addEventListener handler.
- 5
Fix the unnecessary stopPropagation
easy8 minChecking the target is enough, and leaves other handlers working.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace