Reset field HTML Img

0

Cordial Greeting,

I hope you are well and can help me with what I want to do.

I tell you what I do so far.

I have an input file. What it does is load an image and next to it appears the preview of the image that was loaded.

We are doing well so far.

Once I have the preview of the image, I have another button to reset that.

The fact is that he managed to clean the input, but the image is still there ..

I do not know how to make it go away, but at the moment of choosing another image keep working the preview.

This is what I have:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {font-family: Arial, Helvetica, sans-serif;}

#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

/* Caption of Modal Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 400px;
}

/* Add Animation */
.modal-content, #caption {    
    -webkit-animation-name: zoom;
    -webkit-animation-duration: 0.6s;
    animation-name: zoom;
    animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
    from {-webkit-transform:scale(0)} 
    to {-webkit-transform:scale(1)}
}

@keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
</style>
</head>
<body>

<script type="text/javascript">

    $(window).load(function(){

 $(function() {
  $('#file-input').change(function(e) {
      addImage(e); 
     });

     function addImage(e){
      var file = e.target.files[0],
      imageType = /image.*/;

      if (!file.type.match(imageType))
       return;

      var reader = new FileReader();
      reader.onload = fileOnload;
      reader.readAsDataURL(file);
     }

     function fileOnload(e) {
      var result=e.target.result;
      $('#imgSalida').attr("src",result);

     }
    });
  });

</script>

<form id="frm-example" name="frm-example">
   <input name="file-input" id="file-input" type="file" />
   <br />
    <div id="limpiarimg">
   <img id="imgSalida" style="width:100%;max-width:300px" src=""/>
   <div id="myModal2" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img012">
  <div id="caption2"></div>
</div>
</div>
 </form>

  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>



    <script  src="js/index.js"></script>





      <button id="btn-example-file-reset" type="button">Reset file</button>



<script type="text/javascript">
  $('#btn-example-file-reset').on('click', function(e){
   var $el = $('#file-input');
   $el.wrap('<form>').closest('form').get(0).reset();
   $el.unwrap();

});
</script>




<script>
// Get the modal
var modal = document.getElementById('myModal2');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('imgSalida');
var modalImg = document.getElementById("img012");
var captionText = document.getElementById("caption2");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
    modal.style.display = "none";
}
</script>

</body>
</html>

Thanks and I hope you can help me

    
asked by Andres Rodriguez 19.11.2018 в 15:22
source

1 answer

1

The problem is in this part of the code:

$('#btn-example-file-reset').on('click', function(e){
  var $el = $('#file-input');
  $el.wrap('<form>').closest('form').get(0).reset();
  $el.unwrap();
});

In this function the image is empty. You empty the input but the image you have displayed is not linked to it, you are collecting it and showing it but the source ( src ) of said image remains even if it disappears from input . It should be as follows:

$('#btn-example-file-reset').on('click', function(e){
   var $el = $('#file-input');
   $el.wrap('<form>').closest('form').get(0).reset();
   $el.unwrap();

   $("img#imgSalida")[0].setAttribute("src", "");
 });

With:

$("img#imgSalida")[0].setAttribute("src", "");

What you do is reset the src attribute of the image to an empty string.

I leave the code corrected. I recommend that the scripts you initialize (the library of jquery and the call to js/index.js ) do it within head , so that they load the first and you avoid problems later.

$(window).load(function(){
  $(function() {
    $('#file-input').change(function(e) {
      addImage(e); 
    });

    function addImage(e){
      var file = e.target.files[0],
      imageType = /image.*/;

      if (!file.type.match(imageType))
        return;

      var reader = new FileReader();
      reader.onload = fileOnload;
      reader.readAsDataURL(file);
    }

    function fileOnload(e) {
      var result=e.target.result;
      $('#imgSalida').attr("src",result);
    }
  });
}); 


$('#btn-example-file-reset').on('click', function(e){
  var $el = $('#file-input');
  $el.wrap('<form>').closest('form').get(0).reset();
  $el.unwrap();
  
  $("img#imgSalida")[0].setAttribute("src", "");
});

// Get the modal
var modal = document.getElementById('myModal2');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('imgSalida');
var modalImg = document.getElementById("img012");
var captionText = document.getElementById("caption2");

img.onclick = function(){
  modal.style.display = "block";
  modalImg.src = this.src;
  captionText.innerHTML = this.alt;
}

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}
body {font-family: Arial, Helvetica, sans-serif;}

#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

/* Caption of Modal Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 400px;
}

/* Add Animation */
.modal-content, #caption {    
    -webkit-animation-name: zoom;
    -webkit-animation-duration: 0.6s;
    animation-name: zoom;
    animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
    from {-webkit-transform:scale(0)} 
    to {-webkit-transform:scale(1)}
}

@keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script  src="js/index.js"></script>
  </head>
  <body>
    <form id="frm-example" name="frm-example">
      <input name="file-input" id="file-input" type="file" />
      <br />
      <div id="limpiarimg">
        <img id="imgSalida" style="width:100%;max-width:300px" src=""/>
        <div id="myModal2" class="modal">
          <span class="close">&times;</span>
          <img class="modal-content" id="img012">
          <div id="caption2"></div>
        </div>
      </div>
    </form>
    <button id="btn-example-file-reset" type="button">Reset file</button>
  </body>
</html>
    
answered by 19.11.2018 / 16:15
source