0

I want to do is put a default image file in the img tag if it is empty or the user havent uploaded his picture yet.

current output: http://jsfiddle.net/LvsYc/3117/

html:

<form id="form1" runat="server">
    <img id="Picture" data-src="#" alt="your image" /> <br />
    <input type='file' id="imgInp" />
</form>

script:

$(function () {
    var loader = 'http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg';
    $('img[data-src]:not([src])').each(function () {
        console.log(this);
        var $img = $(this).attr('src', loader),
            src  = $img.data('src'),
            $clone = $img.clone().attr('src', src);
        $clone.on('load', function () {
            $img.attr('src', src);
        });
    });
});

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

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

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

$("#imgInp").change(function(){
    readURL(this);
});
user3746626
  • 63
  • 2
  • 7
  • 1
    It is a duplicate of that but I prefer this answer to the one that was selected http://stackoverflow.com/a/168448/822711 – Popnoodles Jun 16 '14 at 23:06
  • @Popnoodles yeah it's simpler to use that option – Tanner Jun 16 '14 at 23:07
  • Could it be as easy as setting the img src to the default image and overriding it only if the user provides/has uploaded an image? – JRulle Jun 18 '14 at 11:58

0 Answers0