views:

68

answers:

3

Like the title states, does anyone out there have a clear way to implement this type of functionality?

Example: If you go to http://weewar.com, in their front page you noticed an ajax module that updates every second. However, all of the new items are added to the top of the list. My question is around that very same functionality.

Does anyone have an easy and clear idea as to how one would implement this functionality?

So far I have a method that initially creates the list, then another method is called in an interval that pulls the most recent data from the server..

However, I'm stuck with, how can I add the new dynamic node to the top of the list.

If you can guide me to where I can find this information or give me an idea as to how I can implement this I will be very happy and grateful :)

Thanks in advanced.

A: 

One way will be to recreate the list using javascript. Its like list.items=newitem+list.items. Sorry for writing a conceptual pseudo code. If you need to know the exact javascript, please send me a reply/comment.

You can also implement the same in the following way also:

var m =document.getElementById(listElement).options.length;
for(var i = m; i>= 0 ; i = i-1) 
    document.getElementById(cmbCategory).options[i] = document.getElementById(cmbCategory).options[i-1];
var opt2 = new Option();
opt2.value="100"; /*new value */
opt2.text="New option text";
document.getElementById(listElement).options[document.getElementById(listElement).options.length] = opt2;
Kangkan
Thanks, I had the same idea, however this method still adds the new item to the bottom of the list. Could you please expand on your pseudo code example?Thanks
ericg
Hi, as requested, I tried to add some code to implement the same. I am sure the code is having some issues at this moment. I shall need to rewrite the same. However you may try to get the same.
Kangkan
Thanks for the help! I will try it.
ericg
+5  A: 

jQuery would make it pretty easy for you. Here's an example:

jQuery

$(document).ready(function(){
  $('<div>News 1</div>').prependTo('#textbox');
  $('<div>News 2</div>').prependTo('#textbox');
  $('<div>News 3</div>').prependTo('#textbox');
  $('<div>News 4</div>').prependTo('#textbox');
});

HTML

<div id="textbox"></div>

Output

News 4
News 3
News 2
News 1

As you can see, the news that was added first gets pushed downwards.

Gert G
Thanks, this method seems pretty straight forward and quick. I was trying to do it in native javascript. However I think that might be more troublesome then I first thought!
ericg
jQuery takes away a lot of the pain with JavaScript. Building a functionality like http://weewar.com wouldn't be too hard, since jQuery comes loaded with proven/tested effects, AJAX functionality etc. It's just to dive in and learn the library. It's time well spent. :)
Gert G
+2  A: 

If you use jQuery you can use jQuery('#list_ID:first-child').prepend(new_item);

If you want to do it the old fashion way, document.getElementById('list_ID').innerHTML = new_item + document.getElementById('list_ID').innerHTML;

Or you can use a more DOM friendly method:

var list_item = document.createElement('li'); list_item.innerHTML="Some Text" document.getElementById('list_ID').insertBefore(list_item, document.getElementById('list_ID').firstChild);

Aaron Harun
Thanks mate, your last one worked out great for me. I am trying not to use jquery for now because I use YUI. However, yui also provides a way in YUI 2, but it isn't very clear how to call the method. The old native js worked perfectly though!Thanks to everyone that answered this question!
ericg