Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVlad Legkowski
9,882 Pointsreturn false
Hi, can someone explain (for dummies) the return false, and what it does in this function?
http://stackoverflow.com/questions/7717527/smooth-scrolling-when-clicking-an-anchor-link
(top answer)
Vlad Legkowski
9,882 PointsI tried to use search by SL and author and cant see it, any chance you can post a link here?
Steven Parker
229,785 PointsUsing preventDefault() only replaces part of what returning false does. It also does the equivalent of a stopPropagation(). See this nice description for the difference.
3 Answers
Steven Parker
229,785 PointsEvents often have a default action that is performed if no handler is defined, or in addition to a defined handler. If a handler returns the value false, the default action is not performed.
Events may also propagate (also called bubbling up), where they trigger each parent of the target element in addition to that element. Returning false from the handler also prevents this propagation from occurring.
So, essentially, returning false means "don't do any other stuff because of this event".
Vlad Legkowski
9,882 PointsSo what would be "any other stuff" in the best answer in StacO? I tried to run it without return false and it went ok
Steven Parker
229,785 PointsIt could be the author was just being cautious and/or applying best practices, but there aren't any defaults or propagation issues that would affect the result.
Vlad Legkowski
9,882 PointsThanks Steven for your replies.
Just final thing - can you give me a good example, where you would personally use return false for a good reason, and tell me what would be the reason?
Thanks
Steven Parker
229,785 PointsThere's a good example in the Treehouse JQuery Basics course. A click event handler is attached to anchor (a) tags, giving them a new behavior. In the handler, event.preventDefault() is called (returning false could also have been used) to prevent the click from also causing the browser to refresh the window with a new location (the default behavior).
Vlad Legkowski
9,882 PointsI did this course, but probably you can imagine stuff is mixed in my head, but this is a great example, thanks a lot for you time Steven.
Steven Parker
229,785 PointsCheers, and happy coding!
Christian Thomsen
13,345 PointsChristian Thomsen
13,345 PointsIf you scroll down one of the comments explains it:
"event.preventDefault();" can replace "return false;" – Andres Separ Jan 25 '14 at 17:25