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

Question

Question

How to substract a field from another one?

asked on August 28, 2014 Show version history

Hi everyone,

 

There's a form in which we have 2 fields that get their value from a DB lookup. These values are always integers. We require to substract these two values (the higher one from the lower one). Using the examples in the help files, I have the following code:

$(document).ready(function () {

$('.LPD').change(function () {
      var FPD = parseNumber($('.FPD').val());
      var LPD = parseNumber($('.LPD').val());      
      var PayC = 0;
      PayC = (LPD-FPD+1);
      
      $('.Paycheck input').val(PayC);
  });
 
function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

Where LPD is the higher value and last one to be changed. FPD is the lowest one and Paycheck is the field where we're showing the value.

 

I have tried bypassing the parseNumber() function and using the built in parseInt() value but it didn't work.

 

The interesting part is that the values of LPD and FPD are always '0', no matter what value is added. I've even changed the field type to be number, and removing the lookup rule by explicitly adding a number as a value, and even then it returns 0 as value.

 

Am I missing something here?

0 0

Answer

SELECTED ANSWER
replied on September 2, 2014 Show version history

Why not tell us what you get when you try the below code, but also, post screenshots of the fields with the field numbers and the CSS Classes applied to help us confirm and troubleshoot with you

$(function() {
  $(document).ready(function () {
 
   function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
   $('.LPD').change(function () {
      var FPD = parseNumber($('.FPD input').val());
	  
	  var tempFPD = $('.FPD input').val();
	  alert('FPD Before='+tempFPD+'   FPD After='+FPD);
	  
      var LPD = parseNumber($('.LPD input').val());
	  
      var tempLPD = $('.LPD input').val();
	  alert('LPD Before='+tempLPD+'   LPD After='+LPD);   
	  
      var PayC = 0;
      PayC = ((LPD-FPD)+1);
      
      $('.Paycheck input').val(PayC);
  });

  });
});

EDIT:

 

I dont see this asked before, are the fields in a table? that could change things slightly. I really hope to see what is going on once you try the above code and post a screenshot

1 0

Replies

replied on August 29, 2014
$('.LPD').change(function () {
      var FPD =  $('.FPD input').val() === '' ? 0 : parseFloat($('.FPD input').val());
      var LPD =  $('.LPD input').val() === '' ? 0 : parseFloat($('.LPD input').val());      
      var PayC = 0;
      PayC = ((LPD-FPD)+1);
      
      $('.Paycheck input').val(PayC);
  });

I got this working by making the above changes. I was always getting '1' as the returned value not '0' when testing the original code. I realized you must not be getting the values right. I tried adding in my changes to the ParseNumber function and it is not even being called. 

0 0
replied on August 29, 2014 Show version history

I realized that you needed to change up the way you were calling things and the order. Javascript recognizes functions that are located above itself since it reads the code in order when you are doing everything in .ready()

 

$(document).ready(function () {
 
   function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
   $('.LPD').change(function () {
      var FPD = parseNumber($('.FPD input').val());
      var LPD = parseNumber($('.LPD input').val());      
      var PayC = 0;
      PayC = (LPD-FPD+1);
      
      $('.Paycheck input').val(PayC);
  });

});

This modified code is virtually the same as the original, you just needed to put your external function above the change activities and you also needed to change the value you are passing to include ' input'

replied on August 29, 2014

Hi Kenneth,

 

Thank you for the reply. I tried both ways, bot neither one worked. I've even created a separate form just to test this function but I get the same result.

0 0
replied on August 29, 2014 Show version history

Did you make sure not to copy the code with the numbers from the lines in it? Make sure those are removed.

 

Have you made sure all the capitalizations of the CSS classes are on those fields? Maybe the FPD field isnt applied properly.

 

Try adding in some more function stuff:

$(function() {
  $(document).ready(function () {
 
   function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
   $('.LPD').change(function () {
      var FPD = parseNumber($('.FPD input').val());
      var LPD = parseNumber($('.LPD input').val());      
      var PayC = 0;
      PayC = (LPD-FPD+1);
      
      $('.Paycheck input').val(PayC);
  });

  });
});

Also, What version of Forms are you using?

 

As a troubleshooting experiment, try adding the following line after you make all the variables on line 11 and after line 12

alert ('FPD'+FPD+'; LPD="+LPD+'; PayC='PayC);

 

1 1
replied on August 29, 2014 Show version history

Are you filling number fields with these integers? If so, you shouldn't need to do anything special to get JavaScript to see them as numbers. If you fill single line fields, you have to tell JavaScript that the values are numbers, because it assumes they are strings.

 

Something like this should work, assuming you're using number fields:

 

$(document).ready(function() {
  $('.LDP input, .FDP input').change(function() {
    $('.Paycheck input').val($('.LDP input').val() - $('.FDP input').val());
  });
});

 

 

0 0
replied on August 29, 2014

Hi Eric,

 

I've tried it both ways: single line and numbers. Neither one worked. As Kenneth stated, there's no value being passed on to the variables. It always comes back as 0. I'm running Forms 9.1.1.1522

0 0
replied on August 29, 2014

When do the lookup rules take place? Are you setting the value in another field and causing the lookup that way, or is it automatically happening when the form loads?

0 0
replied on August 29, 2014

Hi Eric,

 

The values are being set on another field, and that triggers the lookup. 

 

I also tried setting these values manually, instead of using a lookup, manually typing them in, but even that way it didn't work either.

 

Regards,

0 0
replied on September 2, 2014

Can you please confirm with us that you are giving the right CSS Classes to these fields? Maybe posting a screenshot of these fields from the CSS / Javascript page so we can see the field numbers as well so we might be able to do something more direct since the CSS Class names are not working out for you. 

 

Have you had any success with using the 'alert' line I told you to try adding? It should indicate what values, if any, you are getting from the fields. 

 

alert ('FPD'+FPD+'; LPD='+LPD+'; PayC='PayC);

 

0 0
SELECTED ANSWER
replied on September 2, 2014 Show version history

Why not tell us what you get when you try the below code, but also, post screenshots of the fields with the field numbers and the CSS Classes applied to help us confirm and troubleshoot with you

$(function() {
  $(document).ready(function () {
 
   function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
   $('.LPD').change(function () {
      var FPD = parseNumber($('.FPD input').val());
	  
	  var tempFPD = $('.FPD input').val();
	  alert('FPD Before='+tempFPD+'   FPD After='+FPD);
	  
      var LPD = parseNumber($('.LPD input').val());
	  
      var tempLPD = $('.LPD input').val();
	  alert('LPD Before='+tempLPD+'   LPD After='+LPD);   
	  
      var PayC = 0;
      PayC = ((LPD-FPD)+1);
      
      $('.Paycheck input').val(PayC);
  });

  });
});

EDIT:

 

I dont see this asked before, are the fields in a table? that could change things slightly. I really hope to see what is going on once you try the above code and post a screenshot

1 0
replied on September 5, 2014

Hi Kenneth,

 

It's not on a table.

 

This code right here did the trick! I really don't know why it wasn't working before, but after I pasted this one it worked as it should. 

 

Thank you very much Kenneth!

0 0
replied on September 2, 2014 Show version history

If it's a table, you need something like this actually:

$(function() {
  $(document).ready(function () {
 
   function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
    
  function updatePaycheck () {
    $('.cf-table-block tbody tr').each(function () {
      var PayC = 0;
      var FPD = parseNumber($(this).find('.FPD input').val());
      var LPD = parseNumber($(this).find('.LPD input').val());
      PayC = ((LPD-FPD)+1);
      $(this).find('.Paycheck input').val(PayC);
    });
  }
                                       
   $('.cf-table-block').change(updatePaycheck);

                                       
  });
});

(this assumes that along with the FPD and LPD being in a table row, the Paycheck field is also in the same row. I tested this function to work though when all three fields are in the table

0 0
replied on September 4, 2014

Hi Edgar, 

 

If your question has been answered, please let us know by clicking the "This answered my question" button on the appropriate response.

 

If you still need assistance with this matter, just update this thread. Thanks!

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

Sign in to reply to this post.