Connecting Tech Pros Worldwide Forums | Help | Site Map

Noobie onclick, confirm, return false ??

GiJeet
Guest
 
Posts: n/a
#1: Jun 27 '08
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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Noobie onclick, confirm, return false ??


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
GiJeet
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Noobie onclick, confirm, return false ??


>The default action of the `click' event is to activate the
Quote:
>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?
VK
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Noobie onclick, confirm, return false ??


On May 19, 11:06 pm, GiJeet <gij...@yahoo.comwrote:
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.
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Noobie onclick, confirm, return false ??


GiJeet wrote:
Quote:
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
Lee
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Noobie onclick, confirm, return false ??


VK said:
Quote:
>
>On May 19, 11:06 pm, GiJeet <gij...@yahoo.comwrote:
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.
Early browsers didn't all support an onclick handler for the submit button.


--

Closed Thread