const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
document.body.appendChild(input);

const preview = document.createElement("img");
preview.alt = "Preview";
preview.style.maxWidth = "250px";
preview.style.display = "none";
document.body.appendChild(preview);

input.addEventListener("change", () => {
  const file = input.files[0];
  if (!file) {
    preview.style.display = "none";
    return;
  }
  preview.src = URL.createObjectURL(file);
  preview.style.display = "block";
  preview.onload = () => URL.revokeObjectURL(preview.src);
});