Copy link to clipboard
Copied
Hello all,
I am still in the process of perfecting an expense sheet for my job. I would like for the grand total to self calculate when the form fields have data entered into them. However, if no data is entered and the user just wants to print a form to hand write their expenses I would like for the Grand Total to NOT print. How do I go about this? I have tried visible but doesn't print and then the calculated total doesn't appear. I have tried print but hidden and that makes it print out a 0 when no data is entered and you can't see the total on the sheet when using it on a computer.
I attached the document for you to see and a .jpg explaining what I am asking to learn how to do. I appreciate your time and thank you.
J
Copy link to clipboard
Copied
Hi @johnsbox
Hope you are doing well and thanks for reaching out.
To achieve the behavior you described, you can use JavaScript to dynamically calculate and display the grand total when data is entered into form fields and hide it when no data is entered. Here's a sample JavaScript code for this purpose
Note: I am not an expert on JavaScript, Please check the code below and see if that helps.
<!DOCTYPE html><html><head><title>Expense Form</title></head><body><form id="expenseForm"><label for="expense1">Expense 1:</label><input type="number" id="expense1" class="expenseInput"><br>
<label for="expense2">Expense 2:</label><input type="number" id="expense2" class="expenseInput"><br>
<label for="expense3">Expense 3:</label><input type="number" id="expense3" class="expenseInput"><br>
<label for="grandTotal">Grand Total:</label><span id="grandTotal"></span><br>
<button type="button" id="calculateButton">Calculate</button></form>
<script>// Get references to form fields and the grand total elementconst expenseInputs = document.querySelectorAll('.expenseInput');const grandTotalElement = document.getElementById('grandTotal');const calculateButton = document.getElementById('calculateButton');
// Add a click event listener to the Calculate buttoncalculateButton.addEventListener('click', calculateGrandTotal);
function calculateGrandTotal() {let total = 0;
// Calculate the total by summing the values of filled input fieldsexpenseInputs.forEach(input => {if (input.value) {total += parseFloat(input.value);}});
// Display the grand total if it's greater than zero, hide it otherwiseif (total > 0) {grandTotalElement.textContent = total;} else {grandTotalElement.textContent = '';}}
// Initially, hide the grand totalcalculateGrandTotal();</script></body></html>
This code creates an expense form with three input fields for expenses, a grand total field, and a Calculate button. When you click the Calculate button, it sums the values of the filled input fields and displays the grand total. If the grand total is zero or no data is entered, it hides the grand total.
~Amal
Copy link to clipboard
Copied
This code is way off. It's not even valid Acrobat JS code.
It's better not to post code at all, than to post incorrect code.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You can use this as a Custom Format script, if the field value is zero, it is not displayed (and not printed):
if (event.value && !isNaN(Number(event.value)) && event.value != 0) {AFNumber_Format(2, 0, 0, 0, "$ ", true);}
else {event.value = "";}
See attachment.