Skip to content

Instantly share code, notes, and snippets.

@liranop
Last active January 29, 2024 04:44
Show Gist options
  • Save liranop/fda8b6ec17a2d3b2722d8eec60149f26 to your computer and use it in GitHub Desktop.
Save liranop/fda8b6ec17a2d3b2722d8eec60149f26 to your computer and use it in GitHub Desktop.
JetFormBuilder & media field - hide empty space above the input when no selected file
<script>
document.addEventListener("DOMContentLoaded", function () {
const fileInputs = document.querySelectorAll(".jet-form-builder-file-upload__input");
function toggleFileContentVisibility(fileContainer) {
const fileContent = fileContainer.querySelector(".jet-form-builder-file-upload__content");
const files = fileContainer.querySelectorAll(".jet-form-builder-file-upload__files .jet-form-builder-file-upload__file");
if (files.length < 1) {
fileContent.style.display = "none";
} else {
fileContent.style.display = "flex"; // or "block" depending on your layout
}
}
function handleInputChange(event) {
const fileContainer = event.target.closest(".jet-form-builder-file-upload");
if (fileContainer) {
toggleFileContentVisibility(fileContainer);
}
}
// Initial toggle based on the existing file count for each file upload field
fileInputs.forEach(function (fileInput) {
const fileContainer = fileInput.closest(".jet-form-builder-file-upload");
if (fileContainer) {
toggleFileContentVisibility(fileContainer);
}
});
// Event delegation for input change
document.addEventListener("change", handleInputChange);
// Use MutationObserver to dynamically update file count
const observer = new MutationObserver(function (mutationsList) {
mutationsList.forEach(function (mutation) {
if (mutation.type === "childList") {
const fileContainer = mutation.target.closest(".jet-form-builder-file-upload");
if (fileContainer) {
toggleFileContentVisibility(fileContainer);
}
}
});
});
// Set up observer for changes in the file upload container
fileInputs.forEach(function (fileInput) {
const fileContainer = fileInput.closest(".jet-form-builder-file-upload");
if (fileContainer) {
observer.observe(fileContainer, { childList: true, subtree: true });
}
});
});
</script>
@liranop
Copy link
Author

liranop commented Jan 29, 2024

Paste this code in HTML widget/block with the form and no more empty space above the input when no selected file.

Screen Shot 2024-01-29 at 5 57 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment