Noobie onclick, confirm, return false ??

G

GiJeet

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
 
T

Thomas 'PointedEars' Lahn

GiJeet said:
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.
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.
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.
so, if the user click OK, a 1 is returned

`true' is returned.
but the ! turns it to false
Correct.

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.
and then there's a return false right after the confirm function.

Doesn't matter.
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
 
G

GiJeet

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?
 
V

VK

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.
 
T

Thomas 'PointedEars' Lahn

GiJeet said:
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
 
L

Lee

VK said:
<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.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top