0

Tell me, please, how can a photo be inserted to <img>, when I open <input type = "file"> and choose any file ? How can I use JS / JQuery ?

charlietfl
  • 164,229
  • 13
  • 110
  • 143
Spiderman5
  • 23
  • 5
  • Possible duplicate of [Preview an image before it is uploaded](http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) – Aboodz Feb 28 '16 at 13:55
  • 1
    Possible duplicate of [Loading an image to a from ](http://stackoverflow.com/questions/3814231/loading-an-image-to-a-img-from-input-file) – Vincent Orback Feb 28 '16 at 14:21

1 Answers1

0

Use 'FileReader' object:

$("#yourinput").change(function () {
    var file = this.files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        $("#yourimg").attr("src", e.target.result);
    }
    reader.readAsDataURL(file);
});
MBN
  • 898
  • 6
  • 17