Noobie onclick, confirm, return false ??

Discussion in 'Javascript' started by GiJeet, May 19, 2008.

  1. GiJeet

    GiJeet Guest

    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
    GiJeet, May 19, 2008
    #1
    1. Advertising

  2. GiJeet wrote:
    > 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
    --
    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
    Thomas 'PointedEars' Lahn, May 19, 2008
    #2
    1. Advertising

  3. GiJeet

    GiJeet Guest

    >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?
    GiJeet, May 19, 2008
    #3
  4. GiJeet

    VK Guest

    On May 19, 11:06 pm, GiJeet <> 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.
    VK, May 19, 2008
    #4
  5. GiJeet wrote:
    >> 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
    Thomas 'PointedEars' Lahn, May 20, 2008
    #5
  6. GiJeet

    Lee Guest

    VK said:
    >
    >On May 19, 11:06 pm, GiJeet <> 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.


    --
    Lee, May 20, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan

    OnClick="return confirm" not work

    Alan, May 2, 2005, in forum: ASP General
    Replies:
    1
    Views:
    371
    Evertjan.
    May 2, 2005
  2. Replies:
    10
    Views:
    299
    Thomas 'PointedEars' Lahn
    Feb 16, 2006
  3. Jake Barnes
    Replies:
    1
    Views:
    158
  4. Replies:
    11
    Views:
    285
    Thomas 'PointedEars' Lahn
    Dec 11, 2006
  5. Terry
    Replies:
    1
    Views:
    96
    Thomas 'PointedEars' Lahn
    Oct 15, 2007
Loading...

Share This Page