RoyalSwish
RoyalSwish

Reputation: 1565

Javascript - Call Two Functions at Onsubmit

I have two functions; one that validates the form, the other executes some code if the first function returns true.

<form name="calculator" method="POST" onsubmit="return checkForm(); result(trueOrFalse);" onreset="clearForm()">

The checkForm() function is defined as follows:

function checkForm() {
    var valid = true;
    var radios1 = document.getElementsByName("item");
    var radios2 = document.getElementsByName("postage");

    if (document.getElementById("constituency").value == 0) {
        document.getElementById("constituency").style.border = "1px solid #a60f28";
        document.getElementById("constituency").style.borderRadius = "2.5px";
        document.getElementById("conWarning").style.display = "block";
        valid = false;
    }

    //similar code as above for other fields

    trueOrFalse = valid;
    return valid;
}

The result(valid) function is defined as:

function result(valid) { //use trueOrFalse as parameter
    if (valid == true) {
        document.getElementById("result").style.display = "none";
        document.getElementById("result").innerHTML = "Yay"; //test display value
        document.getElementById("result").style.display = "inline";
    }
    else {
        document.getElementById("result").style.display = "none";
        document.getElementById("result").innerHTML = "0";
        document.getElementById("result").style.display = "inline";
    }

    return valid;
}

The goal here is to check first if the form filled out by the user is valid; if it is then produce a value to show to the user in the HTML as follows:

<div id="pound">&#163; <div id="result">0</div></div>

I'm not sure if the way I'm calling my two functions is correct/legal or if it is, whether the value to be display by the result(valid) function has been written correctly. The form is supposed to act as a calculator.

Upvotes: 0

Views: 330

Answers (1)

Cerbrus
Cerbrus

Reputation: 72837

Instead of:

trueOrFalse = valid;
return valid;

Try this:

return result(valid);

Then remove result(trueOrFalse); from your onsubmit.

Upvotes: 3

Related Questions