ChatGPT helped me with some of this, but I think my next step is that I need the xml to capture the data too.
If our server is hosted with XMPie, would this be something I need to contact support for, or is it something I can update on my end?
JavaScript and CSS to create the conditionally required field, and CSS to style.
-JavaScript-
<script type="text/javascript">document.addEventListener('DOMContentLoaded', function () {
// Identify the shipping method dropdown
const shippingMethodDropdown = document.getElementById('ctl00_cphMainContent_cmbServices');
// Create the new input field for the UPS or FedEx account number
const accountNumberField = document.createElement('input');
accountNumberField.type = 'text';
accountNumberField.id = 'accountNumber';
accountNumberField.name = 'accountNumber';
accountNumberField.placeholder = 'Enter your UPS or FedEx account number';
accountNumberField.style.display = 'none'; // Initially hidden
accountNumberField.required = false; // Initially not required
// Add the new input field to the DOM, right after the shipping method dropdown
shippingMethodDropdown.parentElement.appendChild(accountNumberField);
// Function to check the selected shipping method and toggle the account number field
function checkShippingMethod() {
const selectedOption = shippingMethodDropdown.options[shippingMethodDropdown.selectedIndex];
if (selectedOption.text.includes('BILL TO MY UPS ACCOUNT') || selectedOption.text.includes('BILL TO MY FedEx ACCOUNT')) {
accountNumberField.style.display = 'block';
accountNumberField.required = true;
} else {
accountNumberField.style.display = 'none';
accountNumberField.required = false;
}
}
// Attach an event listener to the dropdown to monitor changes
shippingMethodDropdown.addEventListener('change', checkShippingMethod);
// Initial check in case the default selection is one of the specified options
checkShippingMethod();
// Identify the Next button
const nextButton = document.getElementById('ctl00_cphMainContentFooter_btnNext');
// Function to validate the form before submission
function validateForm(event) {
// Perform the shipping method check
checkShippingMethod();
// If the account number field is visible and required, ensure it is not empty
if (accountNumberField.required && accountNumberField.style.display === 'block' && accountNumberField.value.trim() === '') {
alert('Please enter your UPS or FedEx account number.');
event.preventDefault(); // Prevent the form submission
}
}
// Attach the validation function to the Next button click event
nextButton.addEventListener('click', validateForm);
});
</script>
-CSS-#accountNumber {
margin-top: 10px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
width: 100%;
}