Skip to content

Instantly share code, notes, and snippets.

@sandhikagalih
Created June 13, 2024 15:05
Show Gist options
  • Save sandhikagalih/4bf3985400e062e0867316921375630d to your computer and use it in GitHub Desktop.
Save sandhikagalih/4bf3985400e062e0867316921375630d to your computer and use it in GitHub Desktop.
Javascript Image Preview
const input = document.getElementById('avatar');
const previewPhoto = () => {
const file = input.files;
if (file) {
const fileReader = new FileReader();
const preview = document.getElementById('avatar-preview');
fileReader.onload = function(event) {
preview.setAttribute('src', event.target.result);
}
fileReader.readAsDataURL(file[0]);
}
}
input.addEventListener("change", previewPhoto);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment