• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Add tick box which adds 3% to total

Add tick box which adds 3% to total 1 year 4 months ago #43859

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
I have created a donation form and would like to add a tick box which adds 3% of the donated amount to the total to cover card fees (I want to make it optional for the donor)

I keep thinking this should be simple but I'm missing the trigger
Thanks
Last Edit: 1 year 4 months ago by samier.
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 4 months ago #43861

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
I have a partial solution. I created an extra donation field called threepercent then created a calculation with that as the result using

(({Amount:value} * 1.03) - {Amount:value})*10

then I hid the field using css
and now i have 3 percent added to my total. not ideal as I don't want to make it mandatory - I still want a tick box to make it optional
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 4 months ago #43862

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
Simple answer would perhaps be to use conditional fields. Try this, create two dropdowns 'stuff' and 'stuff1' where stuff contains values and items like 0|-Select Option-[c], 10|item 1, 15|item 2 etc and stuff1 contains the same + 3% so 10.30|item1. Create a checkbox, lets call it add_donation containing an item ' yes add 3%. Then create two conditions to show stuff if checkbox id not 'yes add 3%' and another to show stuff1 if checkbox is 'yes add 3%'.
The only downside is that the add_donation checkbox needs to come before the stuff fields
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
Last Edit: 1 year 4 months ago by iceferret. Reason: amendment
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 4 months ago #43868

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
I added a checkbox with Yes already ticked (Yes|Yes[c]) and with the conditions that the 3% field is hidden when unticked and shown when ticked.
Unticking the box takes away the 3% as it should but ticking it again does not add the 3% back
what am i doing wrong?
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 4 months ago #43869

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
Not sure, the idea works for me. As long as you have two product fields one of which is 3% more that the other and you show the +3% when the box is ticked and show the other product field when it is unticked you should be right.
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 3 months ago #43880

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
Thanks for your Idea - it definitely has merit but this is a donation form and I don't have control over the amounts - I still have to get the donor to have the option of adding the 3%(card fee) or the 3% amount coming out of the donation.
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 3 months ago #43881

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
Ok here's another way that lets the user input the amount. I created 3 fields, donation (textbox), add_donation (checkbox), and total (textbox). Added a new calculation total:value=donation:value then added the code below in form properties/javascript
function updateDonationValue() {
    const donationInput = document.getElementById("donation");
    const addDonationCheckbox = document.getElementById("add_donation0");
    const totalInput = document.getElementById("total");
 
    if (donationInput && totalInput) {
        let donationValue = parseFloat(donationInput.value) || 0;
 
        if (addDonationCheckbox && addDonationCheckbox.checked) {
            totalInput.value = (donationValue * 1.03).toFixed(2); // Add 3%
        } else {
            totalInput.value = donationValue.toFixed(2); // Revert to original value
        }
    }
}

call this in the additional attributes of the checkbox with onClick="updateDonationValue();"

What you have now is a siituation where the user decides the amount and whether to add the 3% the addition of whichis shown in the total field and the originsl amount in the donation field. To get the total to your payment gateway refer to the instructions here, specifically step 3

https://www.rsjoomla.com/support/documentation/rsform-pro/custom-scripting/create-a-payment-form-using-the-form-calculations-feature.html
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 3 months ago #43882

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
Following on it occured that the user may go back and change the amount so we need to allow for that so I added a second function in properties/javascript
function uncheckDonationCheckboxOnChange(donationFieldId, checkboxId) {
    // Add an event listener to the donation field
    document.getElementById(donationFieldId).addEventListener('input', function() {
        // Uncheck the checkbox when the donation field changes
        document.getElementById(checkboxId).checked = false;
    });
}
 
// Example usage:
// Call the function with your donation field and checkbox IDs in the donation fields additional attributes
// onChange="uncheckDonationCheckboxOnChange('donation', 'add_donation0');"
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 year 3 months ago #43883

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
For interest completed the donation form so I can confirm the payment methods work with the rsfp_total hidden field. Made some final modifications:

removed the condition total:value=donation:value to prevent javascript functions being overridden
replaced previous function that unchecked the add 3% checkbox with the following function for a more seamless user experience which updates the total depending the selected/unselected state of the checkbox
function handleDonationChange(donationFieldId, checkboxId, totalFieldId) {
    let donationField = document.getElementById(donationFieldId);
    let checkbox = document.getElementById(checkboxId);
    let totalField = document.getElementById(totalFieldId);
 
    function updateTotal() {
        let donationAmount = parseFloat(donationField.value) || 0;
        let totalAmount = checkbox.checked ? donationAmount * 1.03 : donationAmount;
 
        // Ensure total is always two decimal places
        let formattedTotal = totalAmount.toFixed(2);
 
        // Update text-based elements and input fields
        if (totalField.tagName === "INPUT") {
            totalField.value = formattedTotal; // If total is an input field
        } else {
            totalField.textContent = formattedTotal; // If total is a span, div, etc.
        }
    }
 
    // Update total on donation input change
    donationField.addEventListener('input', updateTotal);
 
    // Format donation input when user clicks away (on blur)
    donationField.addEventListener('blur', function () {
        let value = donationField.value.trim();
        donationField.value = value === "" || isNaN(value) ? "0.00" : parseFloat(value).toFixed(2);
        updateTotal();
    });
 
    // Update total when checkbox is toggled
    checkbox.addEventListener('change', updateTotal);
 
    // Initialize total on page load (ensures correct value if form is prefilled)
    updateTotal();
}
 
// Example usage:
// Call the function with donation field, checkbox ID, and totalID  in the donation fields additional attributes
// onChange="handleDonationChange('donation', 'add_donation0', 'total');"
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
Last Edit: 1 year 3 months ago by iceferret.
The administrator has disabled public write access.
The following user(s) said Thank You: gregs, samier

Add tick box which adds 3% to total 1 month 4 weeks ago #44259

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
Hi there

This issue came up again and I am trying your solution. The last time I used the tax function and just made it permanent.

I have to make it optional this time.
here is the edited script
Sorry I am a beginner in this - Am I missing anything or is this all i need, this is under the CSS and Javascript section of the form properties.
a couple of questions -
  1. Do I need to add the attribute to the checkbox
  2. Do I need to do anything in the calculations section
<script>
function handleDonationChange(Amount, add_donation0, Total) {
    let donationField = document.getElementById(Amount);
    let checkbox = document.getElementById(add_donation0);
    let totalField = document.getElementById(Total);
 
    function updateTotal() {
        let donationAmount = parseFloat(donationField.value) || 0;
        let totalAmount = checkbox.checked ? donationAmount * 1.03 : donationAmount;
 
        // Ensure total is always two decimal places
        let formattedTotal = totalAmount.toFixed(2);
 
        // Update text-based elements and input fields
        if (totalField.tagName === "INPUT") {
            totalField.value = formattedTotal; // If total is an input field
        } else {
            totalField.textContent = formattedTotal; // If total is a span, div, etc.
        }
    }
 
    // Update total on donation input change
    donationField.addEventListener('input', updateTotal);
 
    // Format donation input when user clicks away (on blur)
    donationField.addEventListener('blur', function () {
        let value = donationField.value.trim();
        donationField.value = value === "" || isNaN(value) ? "0.00" : parseFloat(value).toFixed(2);
        updateTotal();
    });
 
    // Update total when checkbox is toggled
    checkbox.addEventListener('change', updateTotal);
 
    // Initialize total on page load (ensures correct value if form is prefilled)
    updateTotal();
}
 
// Example usage:
// Call the function with donation field, checkbox ID, and totalID  in the donation fields additional attributes
// onChange="handleDonationChange('donation', 'add_donation0', 'total');"
</script>
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 month 4 weeks ago #44261

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
Okay, so your fields names are amount, add_donation, and total (note lower case) then put this in the javascript section
// This flag ensures the function only runs once
<script>
// (prevents duplicate event listeners if the field is focused multiple times)
let donationInitialized = false;
 
/**
 * Main function to handle donation calculations
 * 
 * @param {string} amountId - ID of the donation input field
 * @param {string} checkboxId - ID of the "add donation" checkbox
 * @param {string} totalId - ID of the total field (input or text element)
 */
function handleDonationChange(amountId, checkboxId, totalId) {
 
    // Stop the function running more than once
    if (donationInitialized) return;
    donationInitialized = true;
 
    // Get references to the form fields using their IDs
    let donationField = document.getElementById(amountId);
    let checkbox = document.getElementById(checkboxId);
    let totalField = document.getElementById(totalId);
 
    // Safety check: if any field is missing, log an error and stop
    if (!donationField || !checkbox || !totalField) {
        console.log("Field not found:", amountId, checkboxId, totalId);
        return;
    }
 
    /**
     * Calculates and updates the total amount
     * - If checkbox is checked, adds 3% to the donation
     * - Ensures result is always formatted to 2 decimal places
     */
    function updateTotal() {
        // Get donation amount (default to 0 if empty or invalid)
        let donationAmount = parseFloat(donationField.value) || 0;
 
        // Apply 3% increase if checkbox is checked
        let totalAmount = checkbox.checked
            ? donationAmount * 1.03
            : donationAmount;
 
        // Format to 2 decimal places (e.g. 10 → 10.00)
        let formattedTotal = totalAmount.toFixed(2);
 
        // Update the total field depending on its type
        if (totalField.tagName === "INPUT") {
            // If it's an input field
            totalField.value = formattedTotal;
        } else {
            // If it's a span, div, etc.
            totalField.textContent = formattedTotal;
        }
    }
 
    // Update total as user types in the donation field
    donationField.addEventListener('input', updateTotal);
 
    // When user leaves the donation field:
    // - Clean and format the value
    // - Ensure it's always a valid number with 2 decimals
    donationField.addEventListener('blur', function () {
        let value = donationField.value.trim();
 
        donationField.value =
            (value === "" || isNaN(value))
                ? "0.00"
                : parseFloat(value).toFixed(2);
 
        updateTotal();
    });
 
    // Update total when checkbox is checked/unchecked
    checkbox.addEventListener('change', updateTotal);
 
    // Run once on initial load (handles pre-filled values)
    updateTotal();
}
</script>

and this in the 'amount' fields additional attributes
onfocus="handleDonationChange('amount', 'add_donation0', 'total')"

and remember to pass this to the payment gateway as here
send calculated total to paymet
This should do the job for you B)
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.
The following user(s) said Thank You: samier

Add tick box which adds 3% to total 1 month 4 weeks ago #44265

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
Dude - this is awesome - I got it working with a bit of fiddling (lack of knowledge on my part - but trail and error is a great teacher).

One Question - is it possible for the Total amount to update once the box is ticked - currently it sends the calculated amount to the payment gateway (which is great)
The administrator has disabled public write access.

Add tick box which adds 3% to total 1 month 3 weeks ago #44266

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 277
  • Thank you received: 74
yes that's possible Here's the same function but without the 'total' field instead using RSforms Total field from the payments section of add fields.
Unpublish the exisiting 'total' field and add the new one, call it 'total-donation'
In the amount field attributes change to this
onfocus="handleDonationChange('amount', 'add_donation0' ,'total-donation')"

Now replace your exisitng Javascript with this.
<script>
 
/**
 * RSForm Donation Handler
 * ------------------------
 * Works with RSForm "Total" advanced field system.
 *
 * IMPORTANT:
 * - payment_total_8 = display only (UI)
 * - total-donation   = RSForm submission value
 */
window.handleDonationChange = function(amountId, checkboxId, totalDonationId) {
 
    // ------------------------------------------------------------
    // Get form elements
    // ------------------------------------------------------------
 
    let donationField = document.getElementById(amountId);              // donation input
    let checkbox = document.getElementById(checkboxId);                 // +3% checkbox
    let totalDonationField = document.getElementById(totalDonationId);  // RSForm total field (important)
 
    let paymentTotalSpan = document.getElementById('payment_total_8');  // UI display - you will need to find your forms id and replace mine here
 
    // Safety check
    if (!donationField || !checkbox || !totalDonationField) {
        console.log("Missing RSForm field(s)");
        return;
    }
 
    /**
     * Helper: update input or display element safely
     */
    function updateField(field, value) {
        if (!field) return;
 
        if (field.tagName === "INPUT") {
            field.value = value;
        } else {
            field.textContent = value;
        }
    }
 
    /**
     * Core calculation logic
     * - Reads donation
     * - Applies 3% if checkbox selected
     * - Updates RSForm + UI
     */
    function updateTotal() {
 
        let donationAmount = parseFloat(donationField.value) || 0;
 
        let totalAmount = checkbox.checked
            ? donationAmount * 1.03
            : donationAmount;
 
        let formattedTotal = totalAmount.toFixed(2);
 
        // --------------------------------------------------------
        // RSForm submission value (THIS is what gets saved)
        // --------------------------------------------------------
        updateField(totalDonationField, formattedTotal);
 
        // --------------------------------------------------------
        // UI display (user sees this)
        // --------------------------------------------------------
        if (paymentTotalSpan) {
            paymentTotalSpan.textContent = formattedTotal + " USD";
        }
    }
 
    // Live update while typing
    donationField.addEventListener('input', updateTotal);
 
    // Format on blur
    donationField.addEventListener('blur', function () {
        let value = donationField.value.trim();
 
        donationField.value =
            (value === "" || isNaN(value))
                ? "0.00"
                : parseFloat(value).toFixed(2);
 
        updateTotal();
    });
 
    // Checkbox toggle
    checkbox.addEventListener('change', updateTotal);
 
    // Initial calculation
    updateTotal();
};
 
</script>

Initially it wont work for you until you change the line referencing 'payment_total_8' you will have to inspect your form in the developer console of your browser to find your version of this.
This is a simple way of doing what you ask assuming a donation is the only thing affecting the total.
If you can keep your head when all about you are losing theirs, you obviously don't understand the situation!
The administrator has disabled public write access.
The following user(s) said Thank You: samier

Add tick box which adds 3% to total 1 month 3 weeks ago #44267

  • samier
  • samier's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 10
Thanks for all your help I finally got it working the way I wanted.

I tried using the
onfocus="handleDonationChange('amount', 'add_donation0' ,'total-donation')"
and I just couldn't get it to work (it wouldn't send the calculated amount to the payment processor) - I then tried using total instead of total-donation and it worked (I changed the field back to total).

The donation amount now increases by 3% and it displays the correct amount in the total and sends that amount through to the payment processor.
I will upload a sample form if anybody is interested
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!