Created
April 5, 2025 12:51
-
-
Save bugnumber9/98a9ce55fbc36f69bcaa148fa342ee49 to your computer and use it in GitHub Desktop.
File preview for Elementor form upload field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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