By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,465 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

JS onclick multiple functions

P: 2
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:
Expand|Select|Wrap|Line Numbers
  1. <input type="submit" value="thevalue" name="goodname" onclick="return function_1(this.form);function_2(this.form);" />
  2.  
So I tried only function_1 for the onclick, and put this in a .js file, but it also does not call function_2:
Expand|Select|Wrap|Line Numbers
  1. function function_1(health)
  2. {
  3. if(health.phone.value=="")
  4. {
  5. alert("Please enter your home telephone number.\n");
  6. return false;
  7. }
  8. return true;
  9. function_2();
  10. }
  11.  
  12. function function_2(health)
  13. {
  14.  
  15. if(health.first_name.value.length<2)
  16. {
  17.     alert("Please enter your first name.\n");
  18.     return false;
  19. }        
  20. return true;
  21. }
  22.  
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":

Expand|Select|Wrap|Line Numbers
  1. <input type="submit" value="thevalue" name="goodname" onclick="return callThem(this.form);" />
  2.  
with this in the .js file:

Expand|Select|Wrap|Line Numbers
  1. function callThem()
  2. {
  3. function_1();
  4. function_2();
  5. }
  6.  
  7. function function_1(health)
  8. {
  9. if(health.phone.value=="")
  10. {
  11. alert("Please enter your home telephone number.\n");
  12. return false;
  13. }
  14. return true;
  15. }
  16.  
  17. function function_2(health)
  18. {
  19.  
  20. if(health.first_name.value.length<2)
  21. {
  22.     alert("Please enter your first name.\n");
  23.     return false;
  24. }        
  25. return true;
  26. }
  27.  
How do I call function_2?
Mar 3 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,328
just write a 'wrapper'-function func_3 like this:

Expand|Select|Wrap|Line Numbers
  1. function func_3(health) {
  2.     var val = true;
  3.  
  4.     val = function_1(health);
  5.  
  6.     // in case function_1 returns true we call function_2
  7.     if (val) {
  8.         val = function_2(health);
  9.     }
  10.  
  11.     return val;
  12. }
now just call func_3 in your onclick :)

kind regards
Mar 3 '08 #2

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. function func_3(health)
  2. {
  3. var val = true;
  4. val = function_1(health);
  5. //in case function_1 returns true, we call function_2
  6. if(val)
  7. {
  8. val = function_2(health);
  9. }
  10. return val;
  11. }
  12.  
  13. function function_1(health)
  14. {
  15. if(health.phone.value=="")
  16. {
  17. alert("Please enter your home telephone number.\n");
  18. return false;
  19. }
  20. return true;
  21. }
  22.  
  23. function function_2(health)
  24. {       
  25. if(health.first_name.value.length<2)
  26. {
  27.     alert("Please enter your first name.\n");
  28.     return false;
  29. }        
  30. return true;
  31. }
  32.  
There is currently no other code in this .js file.

Still puzzled.
Mar 3 '08 #3

gits
Expert Mod 5K+
P: 5,328
how do you call it?

kind regards
Mar 4 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.