Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript - Clearing an Element
    primarykey
    data
    text
    <p>I have a script which takes an image about to be uploaded and adds an image preview of the image. The only problem is that if the form is filled out again, the previous image does not clear.</p> <p>The id of the where thumbnail images are being placed is" thumbnail". Could someone help me add a line or two of code to this that clears the div before adding a new image preview?</p> <pre><code>&lt;script&gt; jQuery(function($){ var fileDiv = document.getElementById("upload"); var fileInput = document.getElementById("upload-image"); console.log(fileInput); fileInput.addEventListener("change",function(e){ var files = this.files showThumbnail(files); },false) fileDiv.addEventListener("click",function(e){ $(fileInput).show().focus().click().hide(); e.preventDefault(); },false) fileDiv.addEventListener("dragenter",function(e){ e.stopPropagation(); e.preventDefault(); },false); fileDiv.addEventListener("dragover",function(e){ e.stopPropagation(); e.preventDefault(); },false); fileDiv.addEventListener("drop",function(e){ e.stopPropagation(); e.preventDefault(); var dt = e.dataTransfer; var files = dt.files; showThumbnail(files); },false); function showThumbnail(files){ // clear the div for(var i=0;i&lt;files.length;i++){ var file = files[i] var imageType = /image.*/ if(!file.type.match(imageType)){ console.log("Not an Image"); continue; } var image = document.createElement("img"); // image.classList.add("") var thumbnail = document.getElementById("thumbnail"); image.file = file; thumbnail.appendChild(image) var reader = new FileReader() reader.onload = (function(aImg){ return function(e){ aImg.src = e.target.result; }; }(image)) var ret = reader.readAsDataURL(file); var canvas = document.createElement("canvas"); ctx = canvas.getContext("2d"); image.onload= function(){ ctx.drawImage(image,100,100) } } } }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload