Forums

Calculating a field value

BNRBranding 11 Jun, 2014
I've got a form that I need to calculate a value once the user has put in several measurements. I am missing something simple I am sure, but the calculation is not working. Here is the code I am using -

window.addEvent('domready', function(){
  $('calculate').addEvent('click', function(){
document.forms[0]..value=document.forms[0].Above_a.value*Above_b.value*Above_c.value*5.9;
});
});


Joomla 3.3.1, Chronoforms V4
BNRBranding 11 Jun, 2014
Slight correction, this is the code I am using -


window.addEvent('domready', function () {
    $('calculate').addEvent('click', function () {
        document.forms[0].Above_gallons.value = document.forms[0].Above_a.value * Above_b.value * Above_c.value * 5.9;
    });
});
BNRBranding 11 Jun, 2014
The error I am getting in the console is -

Uncaught TypeError: Cannot read property 'addEvent' of null

BNRBranding 11 Jun, 2014
Ok, last update. I fixed a couple more goody errors and got rid of the above error. No errors now, but still no calculated value showing up. This is the latest code I am using -

window.addEvent('domready', function() {
    $$('calculate').addEvent('click', function() {
        document.forms[0].Above_gallons.value = document.forms[0].Above_a.value * document.forms[0].Above_b.value * document.forms[0].Above_c.value * 5.9;
    });
});
GreyHead 12 Jun, 2014
Hi BNRBranding,

Hmmm . . . it's a bit tricky to know whether to use MooTools or JQuery with Cfv4 in Joomla! 3.

I'd probably switch to jQuery looking forward to future versions . . . that said your code is MooTools so we'll stay with that.

There's a problem with $$('calculate') - that $$ returns an array of elements, I think you probably want $('calculate') if that isn't working then probably your calculate button doesn't have an id set.

It's usually simpler not to use a calculate button but to have the calculation done on the fly when the above_gallons value changes the onKeyup event will do that.

Bob
BNRBranding 12 Jun, 2014
As usual, very astute observations Bob!

You were correct on the $$ vs $. It fixed the problem with the error I was getting because I forgot to add an ID to 'calculate'. I could have sworn I did that, but I missed it. Once I added the ID and removed the extra $ everything started working.

Thanks!
This topic is locked and no more replies can be posted.