473,804 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JS onclick multiple functions

2 New Member
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 3221
gits
5,390 Recognized Expert Moderator Expert
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
lewnussi
2 New Member
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 Recognized Expert Moderator Expert
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
4886
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 event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
2
13278
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. I went ahead and wrote my own bit of code so I'm sharing it here for everyone. Even though it doesn't really disable the button by greying it out, it prevents the multiple submissions which it what I was attempting to prevent all along. ...
6
7176
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 use setAttribute FF and safari work, but IE stops working when i used addEventListener and attachEvent safari stops working and when i tried .onClick,none of them worked being fairly inexperienced (but learning fast), i figure i'm doing it
8
28867
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) { return false }
13
107571
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 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...
2
1897
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 like to call a client side function when the person clicks this image button... I am not using an <input > tag for the image button, and would rather not change it to that.
3
15928
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 are the 2 i need to work for one check box: "toggle(Monday1,true,Sunday1,true)" "addUp(99, 'Monday1')" thanks for your help
1
4086
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 hotspots on the image. One being correct(a) and the other incorrect(b). When the user clicks on the correct hotspot or place on the image, it should score and retain that value until the end of the quiz. At the end of the quiz, there is a submit button...
17
2688
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 things? Shouldn't they say the same thing?
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.