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

Question

Question

JS code runs intermittantly

asked on October 18, 2016 Show version history

Hi!

i have a form that uses some JS to total some fields. I am having trouble with it not running intermittantly.

When it works, it works nicely. The issue is that sometimes it doesn't seem to trigger. The fields remain blank and the end user is able to submit the form with no totals.

In testing it seems to behave where when you fill out a field, it adds up. as you fill out more, it recalculates every time a box changed. However i've seen cases where you have to click back and forth between several boxes to get it to total.

I'm not very familiar with JS.  What triggers the code to run? is there a way of forcing it to run when the user selects 'submit'?

Thanks

Mark.

1 0

Replies

replied on October 18, 2016

It depends on the JavaScript you wrote. Can you share your JavaScript? Also for calculating the total of some fields, can you change it to use the out-of-box calculation that Forms support which will make sure the calculation is correct when fields get updated. 

0 0
replied on October 18, 2016

Hi!

i'm working inside a table for Labour times. Each row has a start and end (essentially just a 4 digit number to give 24hr time)

Each Row has a total which gives me the hours spent.

(It's limited to 1/2 hr blocks via regex on the input fields. This gives hours spent in a decimal, not times. That's what the customer wanted, to match up with another system)

There is then an overall total that adds up all rows.

Can the inbuilt calculations be used in tables with dynamic number of rows? How do i then reference the values in rows that may or may not exist?

Thanks

$(document).ready(function () {
  
  $('.cf-table-block').on('blur', 'input', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
  
  function sumtotal() {
    var hour = 0;
    var rate = 0;
    var total = 0;
    var subtotal = 0;   
    $('.cf-table-block tbody tr').each(function () {
      hour = moment($(this).find('.Labour_END input').val(), 'HHmm');
      rate = moment($(this).find('.Labour_START input').val(), 'HHmm');
      subtotal = hour.diff(rate, 'Hours', true);
      $(this).find('.total input').val(subtotal.toFixed(2));
      total += subtotal;
    });  
    $('.Grandtotal input').val(total.toFixed(2));       
  }
  
    function parseNumber(n) { 
    var f = parseFloat(n); //Convert to float number. 
  	return isNaN(f) ? 0 : f; //treat invalid input as 0; 
  } 
});

 

0 0
replied on October 19, 2016

You need to use JavaScript to sum table values. Your function triggers on the 'blur' event. This means that your function fires off when you are on an table input then lose focus on it by clicking outside the input.  It sounds like you are experiencing this issue on your last row. You should notice when you submit the form the values should update. If you want to mitigate this undesired behavior, you could add in a button under the table,  that would prompt the user to click to update the table values. 

1 0
replied on October 19, 2016
$(document).ready(function () {
  
  $('.cf-table-block').on('input change', sumtotal);
  $(document).on('click', '.form-del-field', sumtotal);
  
  function sumtotal() {
    var hour = 0;
    var rate = 0;
    var total = 0;
    var subtotal = 0;   
    $('.cf-table-block tbody tr').each(function () {
      hour = moment($(this).find('.Labour_END input').val(), 'HHmm');
      rate = moment($(this).find('.Labour_START input').val(), 'HHmm');
      subtotal = hour.diff(rate, 'Hours', true);
      $(this).find('.total input').val(subtotal.toFixed(2));
      total += subtotal;
    });  
    $('.Grandtotal input').val(total.toFixed(2));       
  }
  
    function parseNumber(n) { 
    var f = parseFloat(n); //Convert to float number. 
  	return isNaN(f) ? 0 : f; //treat invalid input as 0; 
  } 
});

 

replied on October 19, 2016

Hi!

thanks for that. i've added a button (called 'recalculate labour')  under the table. it doesn't really do anything except steal focus off the table. i'll have to try and see if this helps.

The issue i'm having is where the user fills out the table. then the rest of the form (about a full page worth) but still can submit it with the labour blank, like the code still hasn't triggered.

Is there anyway of setting up the code to run 'on submit'?

Or perhaps i can hide the submit button until the code is run?

Thanks

0 0
replied on October 20, 2016

So the table is not recalculating when you focus off the table? Is the value correct on the submission side?

0 0
replied on October 20, 2016

The value (if there is one) is correct. i've never seen it mis-calculate. the problem is that it is sometimes blank. the user fills out and submits the form, but the totals are still blank. When the admin staff look at the submitted form in the repository, the totals are blank and they have to add them up manually.

The table for the labour is in between other required fields, so i think its unlikely they are filling it out last and then submitting without losing focus on this table.  Even if they are, the user has to put in start and end values. most of the time, changing focus between these 2 fields within the table gives me a total.

 

0 0
replied on October 21, 2016

The button click did not help. Also you could add the function to the submit button.

You are not allowed to follow up in this post.

Sign in to reply to this post.