[Javascript] Question: Getting 2 functions to work on 1 onclick event

Peter Brunone peter at brunone.com
Wed Aug 8 11:19:37 CDT 2001


Craig,

    I'd recommend calling *one* function that then calls both of these.
Have the first function return true if all is well; then your master
function will look like this:

function masterCall() {
//  This runs function 1 and if it is found to be true, it runs function 2
as well.
    if(function1) {
        function2;
        }

    }
----- Original Message -----
From: "Neal, Craig" <CNeal at dthr.ab.ca>
To: <javascript at LaTech.edu>
Sent: Wednesday, August 08, 2001 10:42 AM
Subject: [Javascript] Question: Getting 2 functions to work on 1 onclick
event


| Any idea how I can get 2 functions to work on the same onclick event?
|
| The first function is a modified Dreamweaver validation script which uses
| alert to pop up a window with information on which form fields have not
been
| completed.
|
| If all fields are complete then it returns no alert window.
|
| function MM_validateForm() { //v4.0
|
| var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
|
| for (i=0; i<(args.length-2); i+=3) { test=args[i+2];
| val=MM_findObj(args[i]);
|
| if (val) { nm=val.name; if ((val=val.value)!="") {
|
| if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
|
| if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail
| address.\n';
|
| } else if (test!='R') {
|
| if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
|
| if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
|
| min=test.substring(8,p); max=test.substring(p+1);
|
| if (val<min || max<val) errors+='- '+nm+' must contain a number between
| '+min+' and '+max+'.\n';
|
| } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is
required.\n'; }
|
| } if (errors) alert('The following error(s) occurred:\n'+errors);
|
| document.MM_returnValue = (errors == '');
|
| }
|
| The second function submits the form information for processing to another
| page I have.
|
| function Save_onclick() {
|
| SaveForm.Page.value = otherDoc.body.outerHTML;
|
| SaveForm.submit();
|
| }
|
| Both functions work flawlessly by themselves.
|
| However I want MM_validateForm() to be processed first and if it returns
no
| errors then I want Save_onclick() to run.
|
| This only runs Save_onclick() and never gets to MM_validateForm()
|
| <input type="button" value="Submit Bulletin" name="Save"
| LANGUAGE="javascript"
|
| onclick="return
|
Save_onclick();MM_validateForm('ndate','','R','title','','R','printdate','',
| 'R','email','','R','moreinfo','','R');return document.MM_returnValue">
|
| and this only runs MM_validateForm() but doesn't run Save_onclick even if
| MM_validateForm returns no errors.
|
| <input type="button" value="Submit Bulletin" name="Save"
| LANGUAGE="javascript"
|
|
onclick="MM_validateForm('ndate','','R','title','','R','printdate','','R','e
| mail','','R','moreinfo','','R');return document.MM_returnValue;return
| Save_onclick()">
|
| I appreciate any assistance on how I write the syntax so that
| MM_validateForm() runs first and if no error messages are generated then
| Save_onclick() runs.
|
| Thanks so much.
|
|




More information about the Javascript mailing list