473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:
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
3 3200
gits
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
how do you call it?

kind regards
Mar 4 '08 #4

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

Similar topics

17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
2
by: techfuzz | last post by:
I scoured this group and others looking for the best way to disable a button after the first click to prevent multiple submissions, but never did find anything that worked like they said it would. ...
6
by: Nx | last post by:
i've got it all working nicely in firefox, but whenever i test it in IE none of the onclick events are triggered. i'm using an xsl to transform an rss feed into a photogallery. when i try to...
8
by: Barkster | last post by:
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)...
13
LacrosseB0ss
by: LacrosseB0ss | last post by:
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...
2
by: dnewton | last post by:
Hi, Can the onlclick event call multiple functions Using ASP.NET I have created an image button that runs my form at server side and makes use of an onClick event... but... I would also...
3
by: macsaregreat | last post by:
How do you apply 2 onclick events to a check box. I googled this and found multiple ways of doing it, but they dont work. Maybe i was not using the correct syntax, but i cant figure it out. these...
1
by: sourcie | last post by:
I am changing an existing quiz found on "JavaScriptKit.com Multiple Choice Quiz" I have an image. Instead of using the radio buttons with the normal true/false question, I want to place two...
17
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.