I'm currently pretty clueless when it comes to code, and I'm self-teaching at the moment. I want to be able to change the image on a webpage when the user selects a file to upload. There are loads of examples of this, I've been playing with the answer here Show an image preview before upload
However I have a page with multiple upload buttons, each with their own associated image. The example above monitors a specific input file element for change, then fires and updates the image element.
I'm trying to create a JS function based on the above that I pass the uploaded file object and the image element id to, called by the onChange event of the file input element.
It feels as if it should be simple, but it is highlighting my severe lack of understanding. Any help greatly appreciated.
edit:
I tried (no laughing please...)
HTML
<input type="file" id="file_n" onChange="swapImage(this,'image_n)/>
<img id="image_n" />
Javascript
function swapImage(e1, imgID) {
var reader = new FileReader();
reader.onload = function (e1) {
// get loaded data and render thumbnail.
document.getElementById(imgID).src = e.target.result;
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
};
Just to be clear file_n and image_n are supposed to be representing multiples of each, e.g. file_1, file_2, file_3 & corresponding image_1, image_2, image_3, hence why in my mind passing them to a function seems the way to go.