Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append an element with fade in effect [jQuery]

var html = "<div id='blah'>Hello stuff here</div>"

$("#mycontent").append(html).fadeIn(999);

This doesn't seem to work.

I just want a cool effect when the content gets appended.

Note: I want just the new "blah" div to fade in, not the entire "mycontent".

like image 948
TIMEX Avatar asked Jan 14 '11 03:01

TIMEX


3 Answers

$(html).hide().appendTo("#mycontent").fadeIn(1000);
like image 95
icktoofay Avatar answered Nov 08 '22 20:11

icktoofay


Adding a little more info:

jQuery implements "method chaining", which means you can chain method calls on the same element. In the first case:

$("#mycontent").append(html).fadeIn(999);

you would be applying the fadeIn call to the object which is target of the method chain, in this case #mycontent. Not what you want.

In @icktoofay's (great) answer you have:

$(html).hide().appendTo("#mycontent").fadeIn(1000);

This basically means, create the html, set it as hidden by default, append it to #mycontent and then fade it in. The target of the method chain now is hmtl instead of #mycontent.

like image 22
Pablo Fernandez Avatar answered Nov 08 '22 20:11

Pablo Fernandez


This also works

$(Your_html).appendTo(".target").hide().fadeIn(300);

Regards

like image 22
Mohd Sakib Avatar answered Nov 08 '22 19:11

Mohd Sakib