user3095297
user3095297

Reputation: 39

jquery doesn't remove last div

I want to append tweet in an empty div, but when the tweet count is 5 and greater, i want remove one from bottom so that only 5 tweet will appear. i wrote this code which doesnt remove anything

var side = 'left';
cnt = 0;
setInterval(function(){
    $('.tweet-stream').prepend('<div class="tweet t'+side+'"></div><div class="t-header"></div>');
    if(cnt%2==0){
        side='right';
    } else {
        side='left';
        if(cnt>=5){
            $("div[class=tweet]:last").remove();
        }
    }
    cnt++;
},3000);

Upvotes: 0

Views: 101

Answers (3)

chonerman
chonerman

Reputation: 135

Looks like you were only removing the last tween when the "left" condition was met.

This seems to be working:

http://jsfiddle.net/chace/dgv8U/10/

var side = 'left';
cnt = 0;
setInterval(function () {
    $(".tweet-stream").prepend("<div class='tweet t" + side + "></div><div class='t-header'>tweet-" + cnt + "</div>");
    if (cnt % 2 == 0) {
        side = 'right';
    } else {
        side = 'left';
    }
    if (cnt >= 5) {
        $("div.tweet:last").remove();
    }
    cnt++;
}, 3000);

Upvotes: 2

Gert B.
Gert B.

Reputation: 2362

$('.tweet-stream:last-child').remove();

Upvotes: 0

jameslafferty
jameslafferty

Reputation: 2182

Use $('div.tweet:last').remove().

Upvotes: 4

Related Questions