I am using socket.io and twitter streaming API.
Now I have to keep the recent three tweets in a list
<ul>
<li></li>
<li></li>
<li></li>
</ul>
My Question is how to do this in Jquery? Whenever there is a new tweet I want to insert it in to first li and move the first li tweet in to second li and so on and remove the 4th tweet.
2
Answers
Use
.prepend()
method in jQuery. This adds HTML element as the first node of the target.And as @Kartikeya suggested, remove the last one like:
.prepend
to insert new element at the beginning of the target element.:gt()
to remove the element if length is greater than expectedlength
.