I am generating a content in “order summary” modal using javascript. As part of the table I want to give a user an option of either generate a order confirmation pdf or attach that pdf from local storage. The attachment function is through a hidden input-type button, that is called using a visible and specifically styled submit button
summaryContainer.innerHTML = '';
for (const [supplierId, group] of supplierGroups) {
const supplierDiv = document.createElement('div');
supplierDiv.className = 'supplier-summary-table';
let supplierHtml = `
<h6>Supplier: ${group.supplier.supplierName}</h6>
`;
// Add order confirmation section
supplierHtml += `
<div class="po-upload-section">
<div>
<h6 class="mb-1">Purchase Order</h6>
</div>
<div class="po-actions">
<button class="btn btn-outline-primary generate-po-btn" data-supplier-id="${supplierId}">
<i class="bi bi-file-earmark-plus"></i> Generate PO
</button>
or
<div class="upload-po" id="upload-po-container-${supplierId}">
<!-- Here I insert the file input function -->
</div>
</div>
<div id="po-status-${supplierId}" class="mt-3"></div>
</div>
`;
supplierDiv.innerHTML = supplierHtml;
summaryContainer.appendChild(supplierDiv);
// Create and attach file input programmatically
const uploadContainer = supplierDiv.querySelector(`#upload-po-container-${supplierId}`);
const uploadButton = document.createElement('button');
uploadButton.className = 'btn btn-outline-secondary';
uploadButton.innerHTML = '<i class="bi bi-upload"></i> Attach PO';
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.pdf';
// fileInput.className = 'form-control';
fileInput.id = `po-upload-${supplierId}`;
fileInput.dataset.supplierId = supplierId;
uploadContainer.appendChild(fileInput);
uploadContainer.appendChild(uploadButton);
fileInput.addEventListener('change', function(e) {
console.log("change event triggered");
}, false);
uploadButton.onclick = (e) => {
e.preventDefault();
fileInput.value = ''; // Reset value to ensure change event
fileInput.click();
};
// Change the style to make it invisible
fileInput.style.opacity = '0';
}
The page renders correctly and when pressing the Attach PO button, file explorer window opens to select the correct file. However, when the file is selected and confirmed, the change event is not triggered, I see no logs in the console related to that. The button also somewhat freezes – clicking it the second time doesn’t open file explorer window.
I am testing this in Firefox 134.0 running in Ubuntu 22.04.
Edit
I forgot to add that searching here hints towards two main reasons that could be the cause for this:
- change event not triggering because an identical file is selected as is the case here, or
- element getting deleted, so
appendChildshould be included to make sure the element is persistent, as is here
In my case, the issue persists even if the input button is clicked for the first time and I indeed have already tried adding appendChil, but to no avail.