Simple math tricks with ColdFusion 8 Ajax tags

A reader yesterday asked me an interesting question. Using ColdFusion 8 and bindings, is it possible to do simple mathematics? For example, he wanted something like this:

<input type="text" name="res" bind="{field1}*{field2}">

While that isn’t possible, you can do it using cfajaxproxy. (Oh my sweet cfajaxproxy - is there anything you cannot do???) Consider the following example:

<cfajaxproxy bind="javascript:doMultiply({first},{second})" /> <script> function doMultiply(x,y) { document.getElementById("result").value = x*y; } </script>

<form> <input id=”first” size=”3”> X <input id=”second” size=”3”> = <input id=”result”>

</form> </code>

The form has 3 fields. The first two are for the numbers I’ll be multiplying. The last field is for the result. I use the cfajaxproxy tag to bind to the first two fields. I run the JavaScript function, doMultiply, whenever first or second changes. And that’s it! I should probably add a check to see if x and y are numeric, but you get the idea.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments