Entity
Entity

Reputation: 8202

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?

Upvotes: 0

Views: 795

Answers (2)

David Tang
David Tang

Reputation: 93664

You want to .empty() the div, before .append():

$('#target').empty().append(im);

Upvotes: 0

amit_g
amit_g

Reputation: 31250

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.

Upvotes: 3

Related Questions