Paul
Paul

Reputation: 11746

How can I append a new div to the last div with the same class name?

All my div blocks use the same class name and I need to append a new div block after a file upload has completed. The way I'm doing it now appends a new block to every existing block. Here is my test code:

HTML:

<div class="img_container" id="img1">EXISTING BLOCK</div>'
<div class="img_container" id="img2">EXISTING BLOCK</div>'

JQuery:

$('#fileupload').bind('fileuploadcompleted', function (e, data) {
    e.preventDefault();
    var $newdiv = $('<div class="img_container" id="img3">NEW BLOCK</div>');

    $('.img_container').append($newdiv);
})

I've also tried appending to the body tag but the new div never appears. Any idea how I can solve this?

Upvotes: 0

Views: 182

Answers (1)

Beat Richartz
Beat Richartz

Reputation: 9622

$('.img_container:last').after($newdiv) // should do the trick

Upvotes: 5

Related Questions