tags:

views:

22

answers:

2

I have a div with a bunch of list items in it. I just want to remove all the contents inside except for the last list item. Is it possible to use $(this) in reference to a div id without putting it inside some sort of click function? Something like this? (I know its wrong)

 $('div[id='+id+']').html($(this).(".list-item:last"));

If not, would I need to make a function and then call it?

$('div[id='+id+']').function(){
 $(this).html($(".list-item:last"));
}
+1  A: 

One way to do this is to use a selector to get the last list item, then remove its siblings. Something like this:

$('div li:last-child').siblings().remove();

Last child: http://api.jquery.com/last-child-selector/

Siblings: http://api.jquery.com/siblings/

Remove: http://api.jquery.com/remove/

ChessWhiz
thanks anyway for tryin man, appreciate it
Scarface
+2  A: 

This should work for what you require, I think:

$('#elementId').children().not(':last-child').remove();

Demo at: JS Bin.

David Thomas
yeah it works, thanks bro
Scarface
Glad to be of help.
David Thomas