Home › Forums › Quform WordPress › Printing the result of a calculation while typing in a single line text field
- This topic has 2 replies, 2 voices, and was last updated 10 years, 4 months ago by
diegocanal.
- AuthorPosts
- March 16, 2015 at 8:34 pm #14655
diegocanal
ParticipantHi,
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,
March 18, 2015 at 10:37 am #14688Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
March 20, 2015 at 11:51 am #14722diegocanal
ParticipantHi Ally,
It works perfect.
Thanks!
Regards,
Diego
- AuthorPosts
- You must be logged in to reply to this topic.