call(back)

preventDefault or stopPropagation?

preventDefault or stopPropagation — what does each do?

Answer out loud in under a minute: a definition, one concrete example, and one pitfall or trade-off.

Hints

Hint 1

Say what still happens after each: does the event keep propagating, does the default action still run?

Solution

Show the approach

Answer

preventDefault cancels the browser's default action, such as following a link or submitting a form, and the event still propagates. stopPropagation stops the event reaching further elements, and the default action still happens. stopImmediatePropagation also skips the remaining listeners on the same element.

Asked at