Two Functions OnClick - Stop if First is False?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Barkster

    Two Functions OnClick - Stop if First is False?

    I have two functions on buttons onclick, they work but if the first
    fails the second still runs. How can I prevent this? Simplified Code
    below. Thanks


    Function Function1 () {
    if (something) {
    return false
    }

    Function Function2 () {
    if (something) {
    return false
    }

    onclick="Functi on1(); Function2()"

  • VK

    #2
    Re: Two Functions OnClick - Stop if First is False?


    Barkster wrote:[color=blue]
    > I have two functions on buttons onclick, they work but if the first
    > fails the second still runs. How can I prevent this? Simplified Code
    > below. Thanks
    >
    >
    > Function Function1 () {
    > if (something) {
    > return false
    > }
    >
    > Function Function2 () {
    > if (something) {
    > return false
    > }
    >
    > onclick="Functi on1(); Function2()"[/color]

    Function Function1 () {
    if (something) {
    return false;
    }
    else {
    return Function2();
    }
    }

    Function Function2 () {
    // do stuff
    return false;
    }

    onclick="return Function1()"

    Comment

    • tim

      #3
      Re: Two Functions OnClick - Stop if First is False?


      VK wrote:[color=blue]
      > Barkster wrote:[color=green]
      > > I have two functions on buttons onclick, they work but if the first
      > > fails the second still runs. How can I prevent this? Simplified Code
      > > below. Thanks
      > >
      > >
      > > Function Function1 () {
      > > if (something) {
      > > return false
      > > }
      > >
      > > Function Function2 () {
      > > if (something) {
      > > return false
      > > }
      > >
      > > onclick="Functi on1(); Function2()"[/color]
      >
      > Function Function1 () {
      > if (something) {
      > return false;
      > }
      > else {
      > return Function2();
      > }
      > }
      >
      > Function Function2 () {
      > // do stuff
      > return false;
      > }
      >
      > onclick="return Function1()"[/color]

      Or

      onclick= "if( Function(1) ) {Function2();}"

      Tim

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Two Functions OnClick - Stop if First is False?

        "Barkster" <bdog4@hotmail. com> writes:
        [color=blue]
        > I have two functions on buttons onclick, they work but if the first
        > fails the second still runs.[/color]

        Yes, nothing in the code to prevent that.
        [color=blue]
        > How can I prevent this?[/color]

        Do you also want the click event to be cancelled if either returns
        false?

        onclick="if (Function1() !== false) { Function2(); }"

        or less strict about the return value of Function1 (the above
        counts only exactly the value "false" as preventing Function2):

        onclick="return Function1() && Function2();"

        It also cancels the click event if Function1 returns false or
        it returns a true-equivalent value and Function2 returns false.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Barkster

          #5
          Re: Two Functions OnClick - Stop if First is False?

          Thanks, I'll try it that way again. I couldn't get it to work that way
          for me. I think I may be confused about putting return in from of the
          function call in the onclick. Mine work without it, can someone tell
          me when to/not to put return in front of the function call? Thanks

          Comment

          • Randy Webb

            #6
            Re: Two Functions OnClick - Stop if First is False?

            VK said the following on 5/9/2006 5:58 PM:[color=blue]
            > Barkster wrote:[color=green]
            >> I have two functions on buttons onclick, they work but if the first
            >> fails the second still runs. How can I prevent this? Simplified Code
            >> below. Thanks
            >>
            >>
            >> Function Function1 () {
            >> if (something) {
            >> return false
            >> }
            >>
            >> Function Function2 () {
            >> if (something) {
            >> return false
            >> }
            >>
            >> onclick="Functi on1(); Function2()"[/color]
            >
            > Function Function1 () {[/color]

            Function Function1()? The JS099 class is that way ====>

            function Function1() might not produce as many errors.

            --
            Randy
            comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
            Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

            Comment

            • ASM

              #7
              Re: Two Functions OnClick - Stop if First is False?

              Barkster a écrit :[color=blue]
              > I have two functions on buttons onclick, they work but if the first
              > fails the second still runs. How can I prevent this? Simplified Code
              > below. Thanks
              >
              >
              > Function Function1 () {
              > if (something) {
              > return false
              > }
              >
              > Function Function2 () {
              > if (something) {
              > return false
              > }
              >
              > onclick="Functi on1(); Function2()"[/color]

              Soluce 1 : onclick="if(!Fu nction1()) Function2();"

              or : onclick="Functi on1();"
              with :
              function Function1 () {
              if (something) return false;
              else Function2();
              }




              Soluce 2 : onclick="if(Fun ction1()) Function2();"

              example :
              function Function1() {
              return confirm('do you agree to continue?');
              }
              function Function2() {
              if (confirm('Did it work?')) alert('Fine !');
              }



              --
              Stephane Moriaux et son [moins] vieux Mac

              Comment

              • Richard Cornford

                #8
                Re: Two Functions OnClick - Stop if First is False?

                Barkster wrote:[color=blue]
                > I have two functions on buttons onclick, they work but if
                > the first fails the second still runs. How can I prevent
                > this? Simplified Code below. Thanks
                >
                > Function Function1 () {[/color]
                ^^^^^^^^
                Should be 'function' in case sensitive javascript. Also, it is
                conventional to use an initial uppercase letter in a function name to
                denote a function acting in the role of a constructor (as the language
                contains no inherent indicator of that condition) so it is best not to
                use initial capitals with non-constructors.
                [color=blue]
                > if (something) {
                > return false
                > }[/color]

                As there is no - else { return true; } - here when something is false
                the Undefined value will be returned, and undefined type-converts to
                false so may have the same effect as returning false in some contexts.

                <snip>[color=blue]
                > onclick="Functi on1(); Function2()"[/color]

                If the functions return true if they do not return false you can do:-

                onclick="return (Function1() && Function2());"

                - so that if the first returns false the second is never tried.

                Note the addition of the return statement within the onclick code, this
                is based upon the assumption that you would want to cancel the onclick
                action if either function returned false. If that is not required it can
                be omitted.

                Richard.


                Comment

                • Barkster

                  #9
                  Re: Two Functions OnClick - Stop if First is False?

                  Thanks for all the info, very helpful.

                  Barclay

                  Comment

                  Working...