$(document).ready(function() { // Initial hide of all coverage amount sections $('.cov-section').hide(); // Check if there's a radio button already checked var initiallyCheckedPolicyType = $('input[name="policy_type"]:checked').val(); if (initiallyCheckedPolicyType) { // Show the corresponding coverage sections based on the initially checked radio button for (var i = 1; i <= initiallyCheckedPolicyType; i++) { $('.cov_id_' + i).show(); } } $('input[name="policy_type"]').change(function() { var selectedPolicyTypeId = $(this).val(); // Hide all coverage amount sections $('.cov-section').hide(); // Show the corresponding coverage sections based on the selected policy type for (var i = 1; i <= selectedPolicyTypeId; i++) { $('.cov_id_' + i).show(); } }); }); document.addEventListener('DOMContentLoaded', function() { // Loop through the sets of date-related inputs for (let i = 1; i <= policyTypeCount; i++) { const startDateInput = document.getElementById(`startDate${i}`); const endDateInput = document.getElementById(`endDate${i}`); const daysInput = document.getElementById(`days${i}`); flatpickr(startDateInput, { altInput: true, dateFormat: "Y-m-d", altFormat: "d-m-Y", allowInput: true, wrap: true, onChange: function(selectedDates) { if (selectedDates.length > 0) { // Set the end date as the start date plus 365 days const adjustedDate = new Date(selectedDates[0]); adjustedDate.setDate(adjustedDate.getDate() + 365); // Update the End Date field endDateInput._flatpickr.setDate(adjustedDate); // Calculate and update the days calculateDays(); } else { endDateInput._flatpickr.clear(); daysInput.value = ''; } }, }); flatpickr(endDateInput, { altInput: true, dateFormat: "Y-m-d", altFormat: "d-m-Y", allowInput: true, wrap: true, readOnly: true, // Add this line to set readonly onChange: calculateDays, }); function calculateDays() { const startDate = startDateInput._flatpickr.selectedDates[0]; const endDate = endDateInput._flatpickr.selectedDates[0]; if (startDate && endDate) { // Calculate the days difference const timeDifference = endDate.getTime() - startDate.getTime(); const daysDifference = Math.ceil(timeDifference / (1000 * 3600 * 24)); daysInput.value = daysDifference; } else { daysInput.value = ''; } } } }); $(document).ready(function() { // Add a new CSS rule to override the padding for the 'section' element $('').appendTo('head'); });