Skip to content

Instantly share code, notes, and snippets.

@bugnumber9
Created April 5, 2025 12:51
Show Gist options
  • Select an option

  • Save bugnumber9/98a9ce55fbc36f69bcaa148fa342ee49 to your computer and use it in GitHub Desktop.

Select an option

Save bugnumber9/98a9ce55fbc36f69bcaa148fa342ee49 to your computer and use it in GitHub Desktop.
File preview for Elementor form upload field
document.addEventListener('DOMContentLoaded', function () {
const fileInput = document.querySelector('input[type="file"]');
const previewContainer = document.createElement('div');
fileInput.insertAdjacentElement('afterend', previewContainer);
fileInput.addEventListener('change', function (e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
previewContainer.innerHTML = `
<div style="display: flex; justify-content: space-between; align-items: center;">
<img src="${e.target.result}" alt="${file.name}" style="max-width: 100px; margin-right: 10px;">
<span>${file.name}</span>
<span class="remove-btn" style="color: red; cursor: pointer;">X</span>
</div>
`;
const removeButton = previewContainer.querySelector('.remove-btn');
removeButton.addEventListener('click', function () {
fileInput.value = ''; // Clear the input
previewContainer.innerHTML = '';
});
};
reader.readAsDataURL(file);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment