jQuery interview question · Most asked
What is the difference between preventDefault(), stopPropagation() and returning false in a jQuery handler?
Summary
event.preventDefault() stops the browser's default action (following a link, submitting a form); event.stopPropagation() stops the event bubbling to ancestor handlers; returning false from a jQuery handler does both.
Longer Answer
Prefer calling preventDefault() explicitly: return false also stops propagation, which silently breaks delegated handlers and analytics listeners higher up the tree. stopImmediatePropagation() additionally prevents other handlers on the same element from running. In plain DOM addEventListener handlers, return false does nothing.
Key Points
- preventDefault: no default action.
- stopPropagation: no bubbling.
- return false: both (jQuery only).
- Prefer explicit preventDefault.
More most-asked jQuery questions
- What is event delegation, and why is it useful?
- How do you make AJAX requests with jQuery?
- What is the difference between $(document).ready(), $(fn), DOMContentLoaded and window.onload?
- What is the difference between .html() and .text(), and why does it matter for security?
- What is the difference between .attr() and .prop()?
- How do you bind and unbind events in modern jQuery? What happened to bind, live and delegate?
- What are jQuery Deferreds, and how do you wait for several requests?
- What is jQuery, and is it still relevant today?