I'm building a photo upload tool where adding a photo generates a new preview div with an input field to name your photo and a canvas
element which I draw their picture to so they can preview it. The idea is to allow them to select multiple photos and preview them before uploading them all at once. The problem I have is when you add a second photo, when it draws the image to the newly generated canvas it clears the contents of all the other canvas elements. Here's my code
function createPhotoPreviewDiv(img){
var canvas_id = "previewCanvas" + $('.previewDivCanvas').length;
var preview = "<div class='previewDiv'><span>Photo 1</span>" +
"<input type='text' placeholder='photo1' class='previewPhotoName'>" +
"<canvas class='previewDivCanvas' id='" + canvas_id + "'></canvas>" +
"</div>";
document.getElementById('editLocationPreviewContainer').innerHTML+= preview;
var canvas = document.getElementById(canvas_id);
canvas.width = 200;
canvas.height = 100;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
}
It works fine, except that when it runs a second time (when the user adds a second photo) then the canvas it had previously created is cleared. Anyone know what I'm doing wrong, or is this expected behavior with multiple canvas elements?