0

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>
4
  • 1
    Shouldn't you be passing more values into Calculate and Calculate2 than just "D" and "B"? Commented Aug 13, 2012 at 19:10
  • 2
    Can you post an SSCCE of your problem on jsfiddle? Commented Aug 13, 2012 at 19:14
  • @ChrisCarew I tried adding in A + C to the compute function, to pass into Calculate and Calculate2, with no luck. I think I may be defining variables in different ways within the functions, causing confusion? Complete code is here: nhpta.com/over-tax-calculator1.4.2.html Commented Aug 13, 2012 at 19:24
  • A point on the site itself: you may find it beneficial to add a "this field required" indicator and somehow show which two fields are/are not required. Commented Aug 13, 2012 at 19:37

2 Answers 2

1

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.");   
    } 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Still no luck, with either this.form[0] or this.forms[0]
Try it with document.getElementById("docForm") where "docForm" is the ID of your form.
@DouglasA.Crosby Thank you - that did the trick, in addition to passing all the variables.
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.