0

I have a input tag of type "File" in my phonegap mobile application. I am selecting only images from that tag. Now my issue was How can we display the selected image in the same HTML, If user select the image the image should display in the HTML. Is it possible?

<input type="file" id="fileUploader" />
<img id="preview" src="#" alt="your image" />

javascript:

function readIMG(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#preview').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#fileUploader").change(function(){
        readIMG(this);
    });

The selected image should display in the "selectedImage" Div. Is it possible? Any suggestions please

Vinod
  • 2,063
  • 7
  • 46
  • 96

1 Answers1

10

Try this method

add image in seleced div

<img id="preview" src="#" alt="your image" />

         function readIMG(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#preview').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#fileUploader").change(function(){
        readIMG(this);
    });

DEMO

Sridhar R
  • 19,414
  • 6
  • 36
  • 35