I have a list which is generated with ajax and I want everytime elements come they go to the top of the list:
My code is:
$(document).ready(function() {
var socket = io.connect('http://0.0.0.0:3000');
var out = "";
socket.on('populate', function(data) {
$.each(data, function(i, obj) {
if(obj['Ping'] == "FALSE"){
out += "<li class='red'><font color='red'>"+obj.Vardas+" is down..."+obj.Data+"</font></li>";
}
else{
out += "<li class='green'><font color='green'>"+obj.Vardas+" is up......."+obj.Data+"</font></li>";
}
});
$("#database > li:first-child").before(out);//.load(blinkColorRed());
});
});
</script>
</head>
<body>
<div style="float:right; overflow-y:scroll; height: 400px; width: 30%">
<ul id ='database'></ul>
</div>
</body>
</html>
Data comes, scroolbox gets generated, but no list elements. If I remove “> li:first-child” list works, but the newest element goes to the bottom as with simple html(‘out’) before(out) doesn not work. Any ideas where I made a mistake?
The reason it doesn’t work with
> li:first-childbecause theulis initially empty so there are no elements to match the#database > li:first-childselector, so you are trying to put the elements before nothing. You could use.prepend()