Lauren
Lauren

Reputation: 47

Embed Javascript function within Javascript function

I need a function to determine: (1) if two inputs have values, throw an error. (2) if neither input has value, throw an error. (3) if input B or input D has a value, determine which Calculate function to then use. Both Calculate functions work, when called as

onClick="Calculate(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"
onClick="Calculate2(this.form.input_A.value, this.form.input_C.value, this.form.input_D.value, this.form)"

But when I use to call the function below:

onclick="compute();"

The errors work, but I'm not calling the Calculate functions right, I'm sure its something obvious but I can't see it. The answer is no longer getting displayed, as it was before the error function was added.

<SCRIPT LANGUAGE="JavaScript">

function compute() {
  var B = document.getElementById("input_B").value;
  var D = document.getElementById("input_D").value;

  if (B != "" && D != "") {
    alert("You may only enter Assessment or Annual Property Taxes.");
  } else if (B != "") {
    Calculate(B);
  } else if (D != "") {
    Calculate2(D);
  } else {
    alert("You must enter a value into either Assessment or Annual Property Taxes.");
  }
}

function Calculate(Atext, Btext, Ctext, form) {
  var A = parseFloat(Atext);
  var B = parseFloat(Btext);
  var C = parseFloat(Ctext);
  form.Answer.value = ((B - A) * C) / 1000;
}

function Calculate2(Atext, Ctext, Dtext, form) {
  var A = parseFloat(Atext);
  var C = parseFloat(Ctext);
  var D = parseFloat(Dtext);
  form.Answer.value = D - ((A * C) / 1000);
}

function ClearForm(form) {
  form.input_A.value = "";
  form.input_B.value = "";
  form.input_C.value = "";
  form.input_D.value = "";
  form.Answer.value = "";
}

</SCRIPT>

Upvotes: 0

Views: 150

Answers (2)

DACrosby
DACrosby

Reputation: 11460

If your compute() function calls Calculate() and Calculate2() with only one parameter sent, you'll need to make new functions that only need one parameter.

function CalculateB(Btext) {
  var B = parseFloat(Btext);
  var form = document.getElementById("docForm");
  form.Answer.value = B / 1000;
}

function CalculateD(Dtext) {
  var D = parseFloat(Dtext);
  var form = document.getElementById("docForm");
  form.Answer.value = D - (1 / 1000);
}

Upvotes: 0

Ray Cheng
Ray Cheng

Reputation: 12576

You are missing the arguments and not calling the functions correctly. Try the following...

function compute() {   
    var B = document.getElementById("input_B").value;   
    var D = document.getElementById("input_D").value;    
    var A = document.getElementById("input_A").value;   
    var C = document.getElementById("input_C").value; 

    if (B != "" && D != "") {     
        alert("You may only enter Assessment or Annual Property Taxes.");   
    } else if (B != "") {     
        Calculate(A, B, C, this.forms[0]);   
    } else if (D != "") {     
        Calculate2(A, C, D, this.forms[0]);   } 
    else {     
        alert("You must enter a value into either Assessment or Annual Property Taxes.");   
    } 
}

Upvotes: 1

Related Questions