You are viewing limited content. For full access, please sign in.

Question

Question

update formulas with javascript

asked on November 30, 2020

I have fields that calculates a total from information in a table. I had to use JavaScript to do this. I used the following code.

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council'))
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other'))
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee'))
    });
  
});

The above totals are working perfectly.

Now, I need to calculate how much is to be paid for each item. Each value needs to be multiplied by a base amount to figure out the amount paid. I have saved base amount in another field, which will be hidden.

Hidden Field

Visible fields

I currently have the Council Meeting Pay set up to use a formula, but it won't update as the Number of Council Meetings change. So I think I need to use JavaScript to do the calculation, but I am beginner when it comes to coding (above code is from using Answers).

Please help.

 

 

0 0

Answer

SELECTED ANSWER
replied on January 4, 2021

Turns out all I had to do was add .change(); to lines 3, 4 & 5 in the original code. This enabled my formulas to work.

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    $('.TotCouncil input[type="text"]').val(countCheck('Event Type','Council')).change();
     $('.TotOther input[type="text"]').val(countCheck('Event Type','Other')).change();
     $('.TotComm input[type="text"]').val(countCheck('Event Type','Committee')).change();
     });
  
});

 

0 0

Replies

replied on December 1, 2020

Since it seems like the base cost doesn't change you can just put it in the calculation in JavaScript without having to get the value from the field.

I would just do the calculation in the on change:

$(document).ready(function() {
  $(document).on('change','#q20 tr td[data-title="Event Type"] input',function(){
    var total1 = countCheck('Event Type','Council');
    $('.TotCouncil input[type="text"]').val(total1);
    var total2 = countCheck('Event Type','Other');
    $('.TotOther input[type="text"]').val(total2);
    var total3 = countCheck('Event Type','Committee');
    $('.TotComm input[type="text"]').val(total3);
    var total = (123.45*total1)+(123.45*total2)+(123.45*total3);
                  //^[or whatever your calculation is]
     $('#council meeting pay ID').val(total);
    });

});

Hopefully this helps!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.