-1

enter image description here

Refer above image , how to clear "Koala.jpg" file name, each time when user hits a delete button , inside a bootstrap modal.

$('#delImg').on({
  click: function() {
    $('#fileupload').attr("value", "");
    $('#fileupload').attr("src", "");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="modal-body">
  <div class="box-body">
    <form method="post" action="#">
      <div>
        <textarea id="" ng-model="" style="" class="textarea" placeholder="Post something"></textarea>
      </div>
    </form>
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <div id="imgdispy"></div>
    <p class="help-block">Example block-level help text here.</p>
  </div>
</div>
<button type="button" id="delImg" class="btn btn-default btn-hover-green" data-action="save" data-dismiss="modal" role="button">Delete</button>
</div>

The above code isn't working

After some google search , clearing cache can be the solution , but need help in understanding syntax and the real cause.

Vikrant
  • 444
  • 1
  • 5
  • 22

1 Answers1

1

You can replace file input with its clone ;

 $('body #exampleInputFile').replaceWith($('body #exampleInputFile').val('').clone(true));

Here is the working code

$('#delImg').on('click', function() {
   // $('#fileupload').attr("value", "");
   // $('#fileupload').attr("src", "");
   
    $('body #exampleInputFile').replaceWith($('body #exampleInputFile').val('').clone(true));

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div class="modal-body">
  <div class="box-body">
    <form method="post" action="#">
      <div>
        <textarea id="" ng-model="" style="" class="textarea" placeholder="Post something"></textarea>
      </div>
    </form>
  </div>
  <div class="form-group">
    <label for="exampleInputFile">File input</label>
    <input type="file" id="exampleInputFile">
    <div id="imgdispy"></div>
    <p class="help-block">Example block-level help text here.</p>
  </div>
</div>
<button type="button" id="delImg" class="btn btn-default btn-hover-green" data-action="save" data-dismiss="modal" role="button">Delete</button>
</div>
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46