document.addEventListener("DOMContentLoaded", function () { var formData = {}; function showTimeInput(containerId) { var checkbox = document.getElementById( containerId.replace("Time", "Checkbox") ); var timeContainer = document.getElementById(containerId); if (checkbox.checked) { timeContainer.style.display = "block"; } else { timeContainer.style.display = "none"; } } function addTime(containerId) { var timeContainer = document.getElementById(containerId); var newLabelFrom = document.createElement("label"); newLabelFrom.textContent = "From:"; var newInputFrom = document.createElement("input"); newInputFrom.type = "time"; newInputFrom.className = "time-input"; var newLabelTo = document.createElement("label"); newLabelTo.textContent = "To:"; var newInputTo = document.createElement("input"); newInputTo.type = "time"; newInputTo.className = "time-input"; timeContainer.appendChild(newLabelFrom); timeContainer.appendChild(newInputFrom); timeContainer.appendChild(newLabelTo); timeContainer.appendChild(newInputTo); updateFormData(); // Call function to update formData when time is added } function updateFormData() { // Reset formData formData = {}; // Loop through each day ["monday", "tuesday"].forEach(function (day) { var fromInputs = document.querySelectorAll( "#" + day + "Time .time-input:nth-child(odd)" ); var toInputs = document.querySelectorAll( "#" + day + "Time .time-input:nth-child(even)" ); var dayData = []; // Check if the day is selected and both time inputs have values for (var i = 0; i < fromInputs.length; i++) { if ( fromInputs[i].value.trim() !== "" && toInputs[i].value.trim() !== "" ) { // Store the data in dayData array dayData.push({ from: fromInputs[i].value, to: toInputs[i].value, }); } } // If there is data for the day, store it in formData if ( document.getElementById(day + "Checkbox").checked && dayData.length > 0 ) { formData[day] = dayData; } }); } function submitForm(event) { event.preventDefault(); updateFormData(); // Ensure formData is updated before submission // Display the collected data (you can replace this with your own logic) alert(JSON.stringify(formData)); } });