7 Replies - 6151 Views - Last Post: 28 May 2011 - 12:25 AM

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Explanation of 'return false'

Posted 27 May 2011 - 11:48 AM

Hi there. i am learning JS at school and it is going pretty well. One concept I seem to be having trouble with is Return False;

I understand that it is used when working with boolean types, but I seem to see it in other places as well, apparently to stop a function from doing something. I was wondering if someone had a good reference that explains the difference uses of return false.

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Explanation of 'return false'

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Explanation of 'return false'

Posted 27 May 2011 - 12:22 PM

This doesn't belong in Student Campus, it belongs in Javascript. Moved there.
Was This Post Helpful? 0
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Explanation of 'return false'

Posted 27 May 2011 - 12:27 PM

Hi,

What I believe you are referring to is returning false upon form submission or something of that nature. In cases like this the return value is preventing the default action of the event handler.


Here is a link from a quick search on the subject. Hope this helps :)
Was This Post Helpful? 2
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Explanation of 'return false'

Posted 27 May 2011 - 12:29 PM

Very often you'll see it in event handlers. for example, onsubmit. return false is a way to tell the event to actually not fire. so in our case, return false onsubmit means that the form is not submitted.

EDIT: Ninjad by Jstall :)

This post has been edited by japanir: 27 May 2011 - 12:30 PM

Was This Post Helpful? 1
  • +
  • -

#5 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Explanation of 'return false'

Posted 27 May 2011 - 03:15 PM

Cool, that makes a lot of sense. But why would you have a submit button that doesn't submit? Is it for occasions when you write your own submit function so the form submits in a different way, or some such thing?
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Explanation of 'return false'

Posted 27 May 2011 - 03:21 PM

When you write a function to validate a form, such function probably returns false if there was some invalid input. The function will return false then, preventing the invalid form from being submitted. And allowing the user to fix the errors, and try to submit again.
Was This Post Helpful? 1
  • +
  • -

#7 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Explanation of 'return false'

Posted 27 May 2011 - 03:29 PM

Ok, makes sense. Thank you all. :bananaman:
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3494
  • View blog
  • Posts: 10,072
  • Joined: 08-June 10

Re: Explanation of 'return false'

Posted 28 May 2011 - 12:25 AM

for working with Event Listeners (.addEventListener() (DOM) / .attachEvent() (IE)), use Event.preventDefault() (DOM) or window.event.returnValue = false (IE)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1