function previewImage() {
var thisElement = event.target
var file = thisElement.files;
var fileReader = new FileReader();
fileReader.onload = function(event) {
thisElement.nextElementSibling.querySelector("img").setAttribute("src", event.target.result);
}
if (file.length) {
fileReader.readAsDataURL(file[0])
}
};
function handleSubmit() {
const imgPath = document.querySelector('input[type=file]').files[0];
const reader = new FileReader();
reader.addEventListener("load", function() {
sessionStorage.setItem("PHOTO", reader.result);
}, false)
if (imgPath) {
reader.readAsDataURL(imgPath);
}
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<div class="row">
<div class="border border-dark border-bottom-0 d-flex align-items-center justify-content-center bg-warning">
<h4 class="d-inline">PHOTO*</h4>
</div>
</div>
<div class="row">
<div class="border border-dark border-bottom-0">
<input type="file" id="file1" accept="image/*" class="form-control" onchange="previewImage();">
<label for="file1" class="d-flex align-items-center justify-content-center">
<img id="display1" class="w-100 h-100">
</label><br>
</div>
</div>
<br>
<div class="d-flex justify-content-center">
<input type="submit" id="submit" onclick="handleSubmit()">
</div>
const o_photo = document.getElementById("display1");
o_photo.src = sessionStorage.getItem("PHOTO");
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<div class="row">
<div class="border border-dark border-bottom-0 d-flex align-items-center justify-content-center bg-warning">
<h4 class="d-inline">PHOTO*</h4>
</div>
</div>
<div class="row">
<div class="border border-dark border-bottom-0" id="o-file1">
<img src="" id="display1" class="w-100 h-100">
</div>
</div>