Skip to main content

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. 1

    Stop a link navigating

    easy10 min

    Cancel the link and report that the page stayed.

  2. 2

    Stop a form reloading

    easy10 min

    Cancel the submit and report that it was handled.

  3. 3

    Check it was prevented

    easy10 min

    Report the value of defaultPrevented after cancelling.

  4. 4

    Stop propagation

    easy10 min

    Stop the click reaching the box and report handled here.

  5. 5

    Filter by target instead

    medium10 min

    Handle only the button, without stopping propagation.

Find the bug(5)

  1. 1

    Fix preventDefault called without brackets

    easy8 min

    It is a method and must be called.

  2. 2

    Fix stopPropagation used to cancel an action

    easy8 min

    Stopping the travel does not stop the browser navigating.

  3. 3

    Fix preventDefault used to stop the bubble

    easy8 min

    The click should not reach the box, which needs stopPropagation.

  4. 4

    Fix return false used to cancel

    easy8 min

    Returning false does nothing in an addEventListener handler.

  5. 5

    Fix the unnecessary stopPropagation

    easy8 min

    Checking 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