Reputation: 998
I have two function and I need to execut in one onClick
. The first one is to confirm the submit, if the user press yes
it should execute the second function. It is not working with me. Any help please.
below my code:
<Script>
function checkSubmit(){
if(confirm('Are you sure you want to submit the data?');)
sendData();
}
</Script>
The button:
<input type="submit" id="send_data" class="send_data" value="Send" onclick="checkSubmit()"/>
THANKS FOR ALL ^_^
Upvotes: 2
Views: 389
Reputation: 9713
It's simply that you have a semi-colon in your if statement. You need something like that :
function checkSubmit() {
var b = confirm('Are you sure you want to submit the data?');
if ( b ) {
sendData();
} else {
return false;
}
}
Edited: If you want to stop the submit of the form you can do this:
<form name="example" action="your url here" method="get" onsubmit="return checkSubmit();">
<input type="text" name="name" />
<input type="submit" id="send_data" class="send_data" value="Send" />
</form>
Upvotes: 1
Reputation: 2495
<Script>
function checkSubmit(){
if(confirm('Are you sure you want to submit the data?')) //you have small error here
sendData();
}
</Script>
Upvotes: 1
Reputation: 20415
You have it almost right. I think your semi-colon is messing you up in your if statement.
function checkSubmit() {
if (confirm('Are you sure you want to submit the data?'))
sendData();
}
function sendData() { alert("data sent"); }
Upvotes: 3
Reputation: 21449
You should use the standard event binding mechanism:
elem.addEventListener('click', yourFunc, false); // for good browsers
elem.attachEvent('onclick', yourFunc); // for old IE versions
and you can add as many listeners as you need
here's a reference:
Upvotes: 1