Jump to content

Recommended Posts

How do I execute two functions on one click:

 

<input type="button" name="Submit" value="CLAIM YOUR KIT"  onClick="CancelChat()" onClick="check(form,form.elements.length)">

 

The problem is that only one fuction executes when I write the code like the above. I need both functions to execute at the same time.

Link to comment
https://forums.phpfreaks.com/topic/93156-2-onclick-functions-on-the-submit-button/
Share on other sites

<input type="button" name="Submit" value="CLAIM YOUR KIT" on="return wrapperFunction(this.form)">

 

<script>
fnction wrapperFunction( eForm ) {
   CancelChat();
   return check(eForm,eForm.elements.length);
}
</script>

 

fenway,

 

I tried that and I get an objected expected error. The problem is that the CancelChat() & check() functions work if they're alone (not being called by the wrapper function). So I don't think its a syntax error within the CancelChat() or check() functions, more along the lines of the function you gave doesn't call the functions correctly. I noticed the "fnction wrapperFunction( eForm ) {

" was spelled incorrectly, so I fixed that, but I still get the same error. I am a newb so I could be wrong here. Thanks for your help.

This is a simple solution.

 

First remove the inline js. Then create a listener for click of that button.

 

function buttonListener(){

document.getElementById('button id').onclick = wrapper;

}

 

function wrapper(){

//your other functions go here

}

 

onload = buttonListener;

For variable scope, variables listen within a function, are local scope and can only be accessed within the function itself, but if you call a variable outside of a function, then it it a global variable and you can access it anywhere.

Not true, in Javascript any variable declared globally (outside a function) is accessible from inside a function unless you specifically declare a local version with var. Here is an example:

 

var test1 = 'declare global 1';
var test2 = 'declare global 2';
var test3 = 'declare global 3';

function runTest ( test3 ) { //Variables declared as arguments are always local
  test1 = 'inside 1'; //Alters the value of the global test1
  var test2 = 'inside 2'; //Local variable, global test2 remains unchanged
  alert("--INSIDE--\ntest1: "+test1+"\ntest2: "+test2+"\ntest3: "+test3);
}

alert("--BEFORE--\ntest1: "+test1+"\ntest2: "+test2+"\ntest3: "+test3);
runTest('argument');
alert("--AFTER--\ntest1: "+test1+"\ntest2: "+test2+"\ntest3: "+test3);

I don't know that that is always true. I declared the following variable outside a function today:

 

var target = document.getElementById("elementName")

 

and was not able to use it inside the function. I had to bring it into the function for it to be usable.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.