tags:

views:

87

answers:

4

I have an UL:

<ul id="news-feed">.....</ul>

I'd like to be able to append a LI to the top of the list and have that appended item slideDown into place.

$("#news-feed").append('<li">This is my item</li>').hide().slideDown();

Problem I'm having is the above code is sliding the news-feed item down, and not the appended item. Any ideas?

+1  A: 

Yes, first you need to make it display none, then make it slide down:

$("#news-feed").append('<li style="display:none">This is my item</li>').find('li:last').slideDown();

If that doesn't work, break it into two statements:

$('#news-feed').append('<li style="display:none">This is my item</li>');
$('#news-feed li:last').slideDown();
Kerry
I'd like the LI to show up at the top?
AnApprentice
So replace append with prepend and your golden
xximjasonxx
The animation isn't smooth and doesn't even slide down? it just bounces
AnApprentice
If you did prepend, you also have to change the `li:last` to `li:first`. I would have to see your page, but I use this method all the time and I have no problems.
Kerry
min-height was messing it up. changing it to just height made it work!
AnApprentice
+3  A: 

Usually we take it this way:

var newsItem = $("<ul></ul>"); // Create a template item and clone from it

var parentItem = $( /* whatever selector string */ );

//  …

newsItem.clone().text("blah").hide().appendTo(parentItem).toggle('slide');

Also, when you say “add to the top of the list”, usually prepend() works better as expected.

Evadne Wu
+4  A: 

If you want to chain it together nicely, flip it around and use prependTo() instead of prepend(). Either:

$("<li>").text("This is my item").hide().prependTo("#news-feed").slideDown();

or

$("<li>This is my item</li>").hide().prependTo("#news-feed").slideDown();

I prefer the first because it handles escaping. The second doesn't. But it's largely a matter of preference.

cletus
+1 - You were quicker, and probably put `.hide()` in a better position. (Not sure if it makes a difference or not.)
patrick dw
+1 for suggesting `prependTo`. If using 1.4+, then `$("<li>", { text: "This is my item" })...` is probably the best combo of both your methods.
Doug Neiner
Why do the hide when you can do style="display:none" inline? Is that faster?
Kerry
@Kerry my philosophy is that if there's an API call for something, use it. Second-guessing libraries, frameworks and compiler's is an irrelevant distraction.
cletus
Bost methods use an API, some are much more efficient than others
Kerry
+2  A: 

If you were hoping to chain it all together, you can use appendTo() instead. It will return the new <li> element so you can slide it down.

$('<li>This is my item</li>').appendTo("#news-feed").hide().slideDown();

EDIT: As @cletus noted, you should use .prependTo() as he did in his answer instead of .appendTo(). This will bring the new item to the top of the list.

patrick dw
+1 You both got this posted before I finished typing my answer. I probably recommend putting the hide sooner like you said in your other comment.
Doug Neiner
@Doug - I think you're right about `.hide()`. And I didn't do the `.prependTo()` either. I thought about it, then forgot about it!
patrick dw
It's to the top of the list so this should be `prependTo()` not `appendTo()`.
cletus
@cletus - Yeah, I know. I thought of it, but then ended up merely rearranging the OP's code, and forgot. You've got it covered in yours. I'll make note of it, though.
patrick dw