JS onclick multiple functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lewnussi
    New Member
    • Mar 2008
    • 2

    JS onclick multiple functions

    I am trying to call more than one function starting with onclick in a form to work in all browsers.

    In the form, this calls only function_1:
    Code:
    <input type="submit" value="thevalue" name="goodname" onclick="return function_1(this.form);function_2(this.form);" />
    So I tried only function_1 for the onclick, and put this in a .js file, but it also does not call function_2:
    Code:
    function function_1(health)
    {
    if(health.phone.value=="")
    {
    alert("Please enter your home telephone number.\n");
    return false;
    }
    return true;
    function_2();
    }
    
    function function_2(health)
    {
         	
    if(health.first_name.value.length<2)
    {
    	alert("Please enter your first name.\n");
    	return false;
    }		
    return true;
    }
    In both cases, the first function works, but not the second, and the Firefox Error Console does not return an error.

    So I tried this in the form, but it produces the error "health has no properties":

    Code:
    <input type="submit" value="thevalue" name="goodname" onclick="return callThem(this.form);" />
    with this in the .js file:

    Code:
    function callThem()
    {
    function_1();
    function_2();
    }
    
    function function_1(health)
    {
    if(health.phone.value=="")
    {
    alert("Please enter your home telephone number.\n");
    return false;
    }
    return true;
    }
    
    function function_2(health)
    {
         	
    if(health.first_name.value.length<2)
    {
    	alert("Please enter your first name.\n");
    	return false;
    }		
    return true;
    }
    How do I call function_2?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    just write a 'wrapper'-function func_3 like this:

    [CODE=javascript]function func_3(health) {
    var val = true;

    val = function_1(heal th);

    // in case function_1 returns true we call function_2
    if (val) {
    val = function_2(heal th);
    }

    return val;
    }[/CODE]
    now just call func_3 in your onclick :)

    kind regards

    Comment

    • lewnussi
      New Member
      • Mar 2008
      • 2

      #3
      I tried that but now when the alert window for function_1 is clicked, the form goes to the "action=" without any entry needing to be made in the form.

      Code:
      function func_3(health)
      {
      var val = true;
      val = function_1(health);
      //in case function_1 returns true, we call function_2
      if(val)
      {
      val = function_2(health);
      }
      return val;
      }
      
      function function_1(health)
      {
      if(health.phone.value=="")
      {
      alert("Please enter your home telephone number.\n");
      return false;
      }
      return true;
      }
      
      function function_2(health)
      {   	
      if(health.first_name.value.length<2)
      {
      	alert("Please enter your first name.\n");
      	return false;
      }		
      return true;
      }
      There is currently no other code in this .js file.

      Still puzzled.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        how do you call it?

        kind regards

        Comment

        Working...