Skip to main content
Inspiring
March 5, 2013
Question

Having trouble with form field math binding

  • March 5, 2013
  • 1 reply
  • 543 views

I have a page that outputs a bunch of rows with form fields from a query. I am trying to bind a totals field to some simple math between a couple of the form fields and cannot get it working for some reason. What I am trying to do is:

bind = {((field1_#id# -field2_#id#) + #field3_#id#)}

What would be the best way to go about this?

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
March 9, 2013

<script type="text/javascript">

function getResult(num1, num2, num3) {

    //converts a blank, space, null, NaN, etc. to 0

    var num1 = num1 || 0;

    var num2 = num2 || 0;

    var num3 = num3 || 0;

    // For decimal numbers, use parseFloat instead

    return parseInt(num1)-parseInt(num2)+parseInt(num3);

}

</script>

<!--- My example, just to simulate your context. --->

<cfset id="x">

<cfform number="mycfform">

    First Number: <cfinput type="text" name="firstnumber_#id#" required="yes"><br>

    Second Number: <cfinput type="text" name="secondnumber_#id#" required="yes"><br>

    Third Number: <cfinput type="text" name="thirdnumber_#id#" required="yes"><br>

    <!--- I have added the 'keyup' event to the last parameter to ensure the result appears immediately, on-key-up.--->

    Calculation Result: <cfinput name="result" type="text" bind="javascript:getResult({firstnumber_#id#},{secondnumber_#id#},{thirdnumber_#id#@keyup})" bindonload="true">

</cfform>