HighDots Forums  

Noobie onclick, confirm, return false ??

Javascript JavaScript language (comp.lang.javascript)


Discuss Noobie onclick, confirm, return false ?? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
GiJeet
 
Posts: n/a

Default Noobie onclick, confirm, return false ?? - 05-19-2008 , 02:06 PM






Hello, I'm trying to figure this code out.

<input type="submit" onclick="if(!confirm('Are you sure?') return
false; "... />

I know that if you return false in an event like this onclick it
prevents the default behavior of the event. So, in the onclick of a
button the default behavior is to submit the form.

Confirm returns the value 1 if the user clicks OK and the value 0 if
the user clicks Cancel and 1 is treated as true and 0 is treated as
false right?

so, if the user click OK, a 1 is returned but the ! turns it to false
and the form should not submit even thou the user click OK, and then
there's a return false right after the confirm function. so what's
happening here? do we even need the return false statement here?

TIA
g

Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Noobie onclick, confirm, return false ?? - 05-19-2008 , 02:24 PM






GiJeet wrote:
Quote:
Hello, I'm trying to figure this code out.

input type="submit" onclick="if(!confirm('Are you sure?') return
false; "... /

I know that if you return false in an event like this onclick it
prevents the default behavior of the event. So, in the onclick of a
button the default behavior is to submit the form.
No, the default behavior is to activate the element.

Quote:
Confirm returns the value 1 if the user clicks OK and the value 0 if
the user clicks Cancel
No, it returns `true' or `false'. Boolean is a type of its own in
ECMAScript implementations.

Quote:
and 1 is treated as true and 0 is treated as false right?
If you mean "evaluated in boolean expressions" by "treated", then you are
correct.

Quote:
so, if the user click OK, a 1 is returned
`true' is returned.

Quote:
but the ! turns it to false
Correct.

Quote:
and the form should not submit even thou the user click OK,
Non sequitur. The default action of the `click' event is to activate the
element, not to submit the form. That is the default action of the form's
submit event, and so must be handled in the `onsubmit' event handler attribute.

Quote:
and then there's a return false right after the confirm function.
Doesn't matter.

Quote:
so what's happening here? do we even need the return false statement here?
The entire code is bogus. However, return window.confirm(...) accomplishes
the same: nothing of consequence here, preventing form submission if the
dialog is canceled within the `onsubmit' attribute value.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee


Reply With Quote
  #3  
Old   
GiJeet
 
Posts: n/a

Default Re: Noobie onclick, confirm, return false ?? - 05-19-2008 , 03:21 PM



Quote:
The default action of the `click' event is to activate the
element, not to submit the form. That is the default action of the form's
submit event, and so must be handled in the `onsubmit' event handler attribute.
so why does the form get submitted when you click OK?


Reply With Quote
  #4  
Old   
VK
 
Posts: n/a

Default Re: Noobie onclick, confirm, return false ?? - 05-19-2008 , 04:07 PM



On May 19, 11:06 pm, GiJeet <gij... (AT) yahoo (DOT) com> wrote:
Quote:
Hello, I'm trying to figure this code out.

input type="submit" onclick="if(!confirm('Are you sure?') return
false; "... /
<input type="submit" onclick="return confirm('Are you sure?');">

should work. Yet there is a long standing tradition to form submit and
not button submit.

<form onsubmit="return confirm('Are you sure?');">

I remember they were some serious reasons for this choice but I don't
remember them now nor I know if they are still actual.


Reply With Quote
  #5  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Noobie onclick, confirm, return false ?? - 05-20-2008 , 03:48 AM



GiJeet wrote:
Quote:
The default action of the `click' event is to activate the
element, not to submit the form. That is the default action of the form's
submit event, and so must be handled in the `onsubmit' event handler attribute.

so why does the form get submitted when you click OK?
I have already explained that in the very paragraph you quoted.

Please leave the attribution line in.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16


Reply With Quote
  #6  
Old   
Lee
 
Posts: n/a

Default Re: Noobie onclick, confirm, return false ?? - 05-20-2008 , 07:30 AM



VK said:
Quote:
On May 19, 11:06 pm, GiJeet <gij... (AT) yahoo (DOT) com> wrote:
Hello, I'm trying to figure this code out.

input type="submit" onclick="if(!confirm('Are you sure?') return
false; "... /

input type="submit" onclick="return confirm('Are you sure?');"

should work. Yet there is a long standing tradition to form submit and
not button submit.

form onsubmit="return confirm('Are you sure?');"

I remember they were some serious reasons for this choice but I don't
remember them now nor I know if they are still actual.
Early browsers didn't all support an onclick handler for the submit button.


--



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.