Brian
Brian

Reputation: 458

How image preview can be removed after it is submitted?

After I preview an image and submit it, the image still remains there with the message comment added. I want the image preview to be removed automatically once it is submitted.

This is my html code:

 function showPreview(objFileInput) {
      if (objFileInput.files && objFileInput.files[0]) {
        var fileReader = new FileReader();
        fileReader.onload = function (e) {
        $("#targetLayer").html('<img src="'+e.target.result+'" width="200px" height="200px" class="upload-preview" />');
        $("#targetLayer").css('opacity','0.7');
        $(".icon-choose-image").css('opacity','0.5');
        }
      fileReader.readAsDataURL(objFileInput.files[0]);
      }
    }

    $("#image_name").change(function(e) {
      showPreview(this);
    });  
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group" id="targetOuter">
      <div id="targetLayer"></div>
      <img src="photo.png"  class="icon-choose-image"  />
      <div class="icon-choose-image">
      <input name="image_name" id="image_name" type="file" class="inputFile"  />
      </div>
    </div>

Upvotes: 0

Views: 672

Answers (1)

Oluwaseye
Oluwaseye

Reputation: 695

//Target the image element and remove 
$(.icon-choose-image).remove();

Upvotes: 1

Related Questions