Jump to content

2 onclick functions on the submit button


ballhogjoni

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

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;

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.