Printing the result of a calculation while typing in a single line text field

Home Forums Quform WordPress Printing the result of a calculation while typing in a single line text field

This topic is: resolved
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14655
    diegocanal
    Participant

    Hi,

    How could I get the calculation in real-time while typing in single line text fields. For example, if I had this, how could I get the calculation while typing in the iphorm_12_5 field:

    <div id='form-total'></div>
    <script>
    jQuery(document).ready(function($) {
    function treatAsUTC(date) {
    var result = new Date(date);
    result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
    return result;
    }

    function daysBetween(startDate, endDate) {
    var millisecondsPerDay = 24 * 60 * 60 * 1000;
    return (treatAsUTC(endDate) - treatAsUTC(startDate)) / millisecondsPerDay;
    }

    var calculate = function() {

    var sDay = $('.iphorm_12_1-input-day').val();
    var sMonth = $('.iphorm_12_1-input-month').val();
    var sYear = $('.iphorm_12_1-input-year').val();

    var eDay = $('.iphorm_12_2-input-day').val();
    var eMonth = $('.iphorm_12_2-input-month').val();
    var eYear = $('.iphorm_12_2-input-year').val();
    var sueldo = $('.iphorm_12_5').val();
    if (sDay != '' && sMonth != '' && sYear != '' && eDay != '' && eMonth != '' && eYear != '') {

    var startDate = new Date(sYear, sMonth - 1, sDay);
    var endDate = new Date(eYear, eMonth - 1, eDay);

    var diff = daysBetween(startDate, endDate);
    var total = diff * sueldo;

    $('#form-total').text('Day difference: ' + total);
    } else {
    $('#form-total').text('');
    }

    };

    // Calculate on page load
    calculate();

    // Recalculate when these date fields are changed
    $('.iphorm_12_1-input-day, .iphorm_12_1-input-month, .iphorm_12_1-input-year, .iphorm_12_2-input-day, .iphorm_12_2-input-month, .iphorm_12_2-input-year, .iphorm_12_5').change(calculate);

    });
    </script>

    Thanks,

    #14688
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #14722
    diegocanal
    Participant

    Hi Ally,

    It works perfect.

    Thanks!

    Regards,

    Diego

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2025 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy