Search code examples
jqueryimage-loading

jQuery set DIV text


I'm having trouble with my code... I have a feeling its merely a careless error... but I cannot, for the life of me, find it. Here's my code:

            var now = new Date();
            var url = "out.jpg?" + now.getTime();
            im = $("<img>");
            im.hide();
            im.bind("load",function(){ $(this).fadeIn(); });
            $('#target').append(im);
            im.attr('src',url);             

This works fine; however I call this code in a loop, and it appends the image over and over and over... I tried using:

    $('#target').text(im);

But that had no effect... Help anyone?


Solution

  • If the target is supposed to contain only this image, you can use html

     $('#target').html(im);
    

    and that would replace the content with the image.