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

Question

Question

Javascript to get value from a filed populate into another field

asked on January 28 Show version history

Hi, I’m not a JavaScript coder; I generated the code using an AI coding tool.

I'm trying to use JavaScript on Forms 10 version to retrieve a value from field-1 and display a calculated result in field-2 with currency formatting and additional text.

Example:
If field-1 contains $50,000, then field-2 should display:
"Changed less than $45,000 or more than $55,000."

However, my current code isn't working as expected. Any help or suggestions would be greatly appreciated!

/*-------------------------------------------------------------*/
// Estimated Income change 
$(document).ready(function() {
  	//  Retrieve Prev Year income field and convert it to a number
  const appPrevIncomeField = document.getElementById('appHHIncome');
  //const appPrevIncomeField = $(".appHHIncome input").val();
  const appHHIncome = parseFloat(appPrevIncomeField.value);
    	
    // Function to format numbers as US currency
  	const formatCurrency = (value) =>
    new Intl.NumberFormat('en-US', { style:'currency', currency:'USD' }).format(value);    

  // Calculate income +10% and income -10%
  const incomeIncrease = appPrevIncome + appPrevIncome * 0.1;
  const incomeDecrease = appPrevIncome - appPrevIncome * 0.1;

  	// Concatenate the  result
  	const result = `Increased Income +10%: ${formatCurrency(incomeIncrease)} |Derased Income -10%: ${formatCurrency(incomeDecrease)}`;
	
  // Set the result into the app_Estimatedincome field
    const estimatedIncomeField = document.getElementById('appEstimatedincome');
  	estimatedIncomeField.value = result;
});

 

LF Forms 2025 RETR -10 pct Income change issue.png
0 0

Replies

replied on January 29

Try this:  

$(document).ready(function() {
    // Retrieve Prev Year income field and convert it to a number
    const appPrevIncomeField = document.getElementById('appHHIncome');
    const appHHIncome = parseFloat(appPrevIncomeField.value);
    
    // Function to format numbers as US currency
    const formatCurrency = (value) =>
        new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value);    

    // Calculate income +10% and income -10%
    const incomeIncrease = appHHIncome + appHHIncome * 0.1;
    const incomeDecrease = appHHIncome - appHHIncome * 0.1;

    // Concatenate the result
    const result = `Increased Income +10%: ${formatCurrency(incomeIncrease)} | Decreased Income -10%: ${formatCurrency(incomeDecrease)}`;
    
    // Set the result into the appEstimatedIncome field
    const estimatedIncomeField = document.getElementById('appEstimatedIncome');
    estimatedIncomeField.value = result;
});

 

0 0
replied on January 29 Show version history

Thank you for your response! Just a quick FYI—I’m not a JavaScript coder; I generated the code using an AI coding GPT tool.  

I copied your code into JavaScript, but it didn’t work as expected. The **TY 2023** field is populated using a field lookup rule, but I’m unsure how this code executes. Could you please advise if there is a need for a specific trigger, such as `.onchange`, or any modifications to the CSS class to ensure it runs properly?  

I’d appreciate any guidance you can provide!  

0 0
replied on January 29

What version of Forms are you using?

0 0
replied on January 29

Forms 10.

0 0
replied on January 30
$(document).ready(function() {
    // Function to format numbers as US currency
    const formatCurrency = (value) =>
        new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(value);

    // Function to calculate and set the income values
    const calculateIncome = () => {
        // Retrieve Prev Year income field and convert it to a number
        const appPrevIncomeField = document.getElementById('appHHIncome');
        const appHHIncome = parseFloat(appPrevIncomeField.value);

        // Calculate income +10% and income -10%
        const incomeIncrease = appHHIncome + appHHIncome * 0.1;
        const incomeDecrease = appHHIncome - appHHIncome * 0.1;

        // Concatenate the result
        const result = `Increased Income +10%: ${formatCurrency(incomeIncrease)} | Decreased Income -10%: ${formatCurrency(incomeDecrease)}`;

        // Set the result into the appEstimatedIncome field
        const estimatedIncomeField = document.getElementById('appEstimatedIncome');
        estimatedIncomeField.value = result;
    };

    // Attach the calculateIncome function to the onchange event of the appHHIncome field
    const appPrevIncomeField = document.getElementById('appHHIncome');
    appPrevIncomeField.onchange = calculateIncome;

    // Optionally, call the function once on document ready to set initial values
    calculateIncome();
});

 

0 0
replied on January 31 Show version history

Carlos, thank you for your time working on this. Unfortunately result didn't return anything back to the field.

I have three questions. 

  • 'TY 2023 income' (appHHIncome) field is currency field, is there a need to convert it to a nuber in Line 10?
  • Per attached form, I need to rerieve value from appHHIncome field, which is filled by the lookup rule first. Then, this function should fill the 'income increase/decrease' (appEstimatedIncome) field. If so, why do we need the Line 20 since we're not retrieveng anything from that field? Does that line assigns the field to estimatedIncomeField as an object?
  • Should I comment out the calculateIncome(); in Line 29 since it's optional?
0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.