Created
June 13, 2024 15:05
-
-
Save sandhikagalih/4bf3985400e062e0867316921375630d to your computer and use it in GitHub Desktop.
Javascript Image Preview
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
| 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