views:

2448

answers:

5

So as the title suggests I wish to create a new dom element (the HTML is generated and retrieved via AJAX). I kind of got it working but it appears hidden, and when I try to Fade In it breaks!

   function AddContent(Content) {
        div = document.createElement(Content)
        div.appendTo($("#contentAreas"));
        //    $(div).fadeIn("slow");
        }

It basically inserts the item into the correct position but doesn't show it. When I attempt to fade it in, it's fails to do so. No errors.

Any ideas?

+5  A: 

Should be $(div).appendTo(...). Or you could change how div is created to div = $(Content), perhaps.

John Kugelman
+2  A: 

The div needs to be jQuery for appendTo to work. Try this:

 function AddContent( content ) {
     $(content).appendTo("#contentAreas").fadeIn("slow");
 }
tvanfosson
thanks! that did it
Damien
Damien, note that appendTo takes a selector so you can just do .appendTo("#contentAreas"). Saves creating another jquery object for no reason
redsquare
@redsquare -- thanks for pointing that out. I've updated as it makes the code cleaner. Internally, though, I think the first thing appendTo does is jQuery(selector).
tvanfosson
@tvanfosson -- indeed, helps save a few keystrokes however!!
redsquare
+3  A: 

You don't need createElement, the jQuery constructor can take html as a parameter (assuming that content is an html string):

function AddContent(content) {
   var div = $(content);
   div.appendTo($("#contentAreas"));
   $(div).fadeIn("slow");
}
Kazar
+3  A: 

How about this:

   function AddContent(Content) {
        div = $('<div>' + Content + '</div>');           
        div.appendTo($("#contentAreas")).fadeIn("slow");
   }
karim79
+1  A: 

The appendTo takes a selector so it does not need to be a jquery object as all the other examples above have it.

function AddContent( content ) {
     $(content).appendTo("#contentAreas").fadeIn("slow");
 }
redsquare