alkhader
alkhader

Reputation: 998

Issue with more multiple functions in one onClick

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

Answers (4)

Miguel Sanchez Gonzalez
Miguel Sanchez Gonzalez

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

Avi Y
Avi Y

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

Code Maverick
Code Maverick

Reputation: 20415

You have it almost right. I think your semi-colon is messing you up in your if statement.

Check out this jsFiddle:

function checkSubmit() { 
    if (confirm('Are you sure you want to submit the data?')) 
        sendData(); 
}

function sendData() { alert("data sent"); }

Upvotes: 3

Headshota
Headshota

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:

https://developer.mozilla.org/en/DOM/element.addEventListener#Legacy_Internet_Explorer_and_attachEvent

Upvotes: 1

Related Questions