Can OnClick call multiple functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LacrosseB0ss
    New Member
    • Oct 2006
    • 112

    #1

    Can OnClick call multiple functions?

    I was wondering, can an OnClick event of a button call multiple functions?

    The reason for this is I have a page that when "ok" is clicked, goes to a preview page before submitting data to the database. If "Modify" is clicked, I want to change session variables (using VB) and then go to the previous page (the easiest way I know how to do this is history.go(-1) in js).

    Can OnClick call both functions? Or is there a way to do this in only VB or JS?

    TIA
    - LB
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    I think the easiest way would be make 2 functions then a 3 that will decide which of the 2 it wants to call.

    Comment

    • LacrosseB0ss
      New Member
      • Oct 2006
      • 112

      #3
      The reason I wanted to do it this way was because I want to use Javascript code as well as VB code. The VB will change session variables and the JS will change the page.

      I have since tried using VB and in the Page_Load method saying
      Code:
      Dim prevPageURL As String = Page.PreviousPage.ToString
      cmdModifyForm.Attributes.Add("PostBackUrl", prevPageURL)
      and then doing the session variables in cmdModifyForm_C lick. We'll see how that goes.

      Thanks again
      - LB

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        You can just call a single function with the onclick, then this will call 2 other function.

        Code:
        <button onclick="javascript: funcCaller();" value="press me" />
        
        <script type="text/javascript">
        
        function funcCaller() {
          funcOne();
          funcTwo();
        }
        
        ...
        
        </script>

        Comment

        • Joe Lay
          New Member
          • Feb 2007
          • 3

          #5
          Above code simply call 2 functions at the same time. I just want to know how to call functions like ELSE IF . if 1 function check finished , another to call


          regards

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            <input type="button" onclick="functi on1(); function2(); function3(); function4(); function5();">

            Comment

            • Joe Lay
              New Member
              • Feb 2007
              • 3

              #7
              i mean it's run all functions at the same time. i just want to run 1st function first. if the 1st function is OK, the second function to work.

              Example is that ----
              _______________ _______________ _______________ _______________ _____
              function formcheck(){
              if (usr==""){
              alert ("Please provide User Name");
              frm.usrid.focus ();
              }
              }

              function numcheck(){

              var NumExpression=/^[0-9]+$/;
              if (!document.frm. scode.value.mat ch(NumExpressio n)){
              alert("Invalid, Number Only");
              frm.scode.selec t();
              return false;
              }

              }

              <input type="button" value="submit" onclick ="formcheck( ); numcheck();">
              _______________ _______________ _______________ _______________ ____

              so if i write like above state, it run all the alert messages. i just want to run formcheck() function first. if it is OK and no more blank " " in Text box , User will click Submit button, then the second numcheck() function will work.
              I don't want to put 2 Submit button. That's the problem.
              so~~~

              How can i call function correctly ? Any1 please

              Comment

              • AricC
                Recognized Expert Top Contributor
                • Oct 2006
                • 1885

                #8
                Originally posted by Joe Lay
                i mean it's run all functions at the same time. i just want to run 1st function first. if the 1st function is OK, the second function to work.

                Example is that ----
                _______________ _______________ _______________ _______________ _____
                function formcheck(){
                if (usr==""){
                alert ("Please provide User Name");
                frm.usrid.focus ();
                }
                }

                function numcheck(){

                var NumExpression=/^[0-9]+$/;
                if (!document.frm. scode.value.mat ch(NumExpressio n)){
                alert("Invalid, Number Only");
                frm.scode.selec t();
                return false;
                }

                }

                <input type="button" value="submit" onclick ="formcheck( ); numcheck();">
                _______________ _______________ _______________ _______________ ____

                so if i write like above state, it run all the alert messages. i just want to run formcheck() function first. if it is OK and no more blank " " in Text box , User will click Submit button, then the second numcheck() function will work.
                I don't want to put 2 Submit button. That's the problem.
                so~~~

                How can i call function correctly ? Any1 please
                Add a boolean var blnIsValid = true then set it to false anytime something doesn't go right at the end check that the variable is true if so go to function 2 if not go back to function 1.

                Comment

                • Joe Lay
                  New Member
                  • Feb 2007
                  • 3

                  #9
                  Originally posted by AricC
                  Add a boolean var blnIsValid = true then set it to false anytime something doesn't go right at the end check that the variable is true if so go to function 2 if not go back to function 1.

                  Please show me example codes or program...

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by Joe Lay
                    Please show me example codes or program...
                    I assume you want something that shows an alert if there is a problem by going through the functions. You don't need the user to press the submit button again if the first validation passed.

                    Just include all your functions in one formCheck function. Then this function can call all the functions one by one. Whenever a function 'fails' (meaning there is an error), just return false, otherwise it continues on till the end:
                    Code:
                    function formCheck {
                    if (!numCheck()) return false;
                    if (!emailCheck()) return false;
                    ...
                    }
                    is one way. There are other ways. For an example, see this. For a different style of form validation, see this link.

                    Comment

                    • aashishn86
                      New Member
                      • Mar 2009
                      • 51

                      #11
                      i want to call
                      onClick="docume nt.pressed=this .value"
                      and
                      onClick="return validate();
                      what do i write ?

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        just wrap both calls in one function and call that function on click ... or write both of the statements separated with a semicolon in one place ...

                        Comment

                        • sumit rawal
                          New Member
                          • Aug 2011
                          • 1

                          #13
                          if there are many validations,the n will you be using multiple functions??

                          Comment

                          • jeevaraj16
                            New Member
                            • Feb 2015
                            • 1

                            #14
                            Code:
                            function Validation3() {
                            	 if(Validation1() !=false)
                            	 {
                            		 Validation2(); 
                            	 }
                            }
                            
                            <input type="submit" onclick="return Validation3()"/>
                            In the first function you have return a Boolean value. Suppose you want call second function only if the first function returns true. you can use the above code. so with out the success of first function you can't call second function
                            Last edited by gits; Feb 19 '15, 09:12 AM. Reason: added code tags

                            Comment

                            Working...