ZaZaKi, a web developer Between Manchester UK & Rotterdam NL. © 2015-2024.

JS display image preview on the form before submit

<input type="file" onchange="previewFile()"><br>
<img src="" height="200" alt="Image preview...">

<script>
function previewFile() {
  var preview = document.querySelector('img');
  var file    = document.querySelector('input[type=file]').files[0];
  var reader  = new FileReader();

  reader.onloadend = function () {
    preview.src = reader.result;
  }

  if (file) {
    reader.readAsDataURL(file);
  } else {
    preview.src = "";
  }
}
</script>
Categroy:
Front-end , javascripts