title: Why does my code not allow uploading the same image file twice in a row?
body: <p>I have tried as best as I could to reduce my code to the absolute minimum. I hope it's not too long. The below code helps upload and remove the preview of image files. The only problem is that it doesn't upload the same image twice in a row. That is, upload image a, remove image a and upload it again. Nothing happens and the default image remains there when I try to upload an image a second time right after removing it. but uploading image a and then replacing it with image b is working without a problem. I couldn't find the bug in my code. I am a beginner learning HTML-javascript and don't know how to solve this.  Any help is appreciated, thanks!</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function changeProfile() {
    $('#image').click();
  }
  
  $('#image').change(function() {
    var imgPath = this.value;
    var ext = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
    if (ext == "gif" || ext == "png" || ext == "jpg" || ext == "jpeg")
      readURL(this);
    else
      alert("Please select image file (jpg, jpeg, png).")
  });
  
  function readURL(input) {
    if (input.files &amp;&amp; input.files[0]) {
      var reader = new FileReader();
      reader.readAsDataURL(input.files[0]);
      reader.onload = function(e) {
        $('#imgPreview').attr('src', e.target.result);
        addDeleteBttn();
      };
    }
  }
  
  function removeImage() {
    $('#imgPreview').attr('src', 'Anzeige%20erstellen-Dateien/default.png');
    removeDeleteBttn();
  }
  
  function addDeleteBttn() {
    document.getElementById("btnDeletePhoto").style.display = "block";
  }
  
  function removeDeleteBttn() {
    document.getElementById("btnDeletePhoto").style.display = "none";
  }
  
  ``
  `</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#bildBttn {
    position: absolute;
    top: 38%;
    left: 18.2%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    background-color: white;
    cursor: pointer;
    border-radius: 5px;
    color: black;
    text-align: center;
    border-color: lightgray;
    height: 50px ! important;
    width: 53px;
    border-radius: 4px;
    padding: 10x 17px;
    border-width: thin
  }
  
  .removeBttnClass {
    position: absolute;
    top: 38%;
    left: 22.7%;
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    background-color: white;
    cursor: pointer;
    border-radius: 5px;
    color: black;
    text-align: center;
    border-color: lightgray;
    height: 50px ! important;
    width: 53px;
    border-radius: 4px;
    padding: 10x 17px;
    border-width: thin
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>```
  
  &lt;!DOCTYPE html  &gt;
  &lt;html lang="en" style="background:  #fffff0;"&gt;
  
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Title&lt;/title&gt;
    &lt;link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"&gt;
    &lt;link rel="stylesheet" media="all" href="upload.css"&gt;
    &lt;script type=text/javascript src="https://code.jquery.com/jquery-3.4.1.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;img id="imgPreview" src="default.png" width="500px" height="360px" style="padding-left:15px;" /&gt;
    &lt;br/&gt;
    &lt;input type="file" id="image" style="display: none;"&gt;
    &lt;!--&lt;input type="hidden" style="display: none" value="0" name="remove"remove"&gt;--&gt;
  
  
    &lt;a href="javascript:changeProfile()"&gt;
      &lt;span class="btn btn-default btn-file" id="bildBttn"&gt;
                          &lt;i title="Bild auswählen" class="fa fileinput-new fa-file-image-o" id="fotoIcon"&gt;&lt;/i&gt;&lt;/span&gt;
    &lt;/a&gt;
    &lt;a id="removeBttnFrame" href="javascript:removeImage()"&gt;
      &lt;div id="btnDeletePhoto" class="removeBttnClass" style="display: none"&gt;&lt;i class="fa fa-trash-o" id="deleteIcon"&gt;&lt;/i&gt;&lt;/div&gt;
    &lt;/a&gt;
  
  
    &lt;script src="uploadScript.js" data-turbolinks-track="reload"&gt;&lt;/script&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>