Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Creating a New DOM element and showing it

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?

like image 446
Damien Avatar asked Dec 07 '22 07:12

Damien


2 Answers

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

like image 149
John Kugelman Avatar answered Dec 11 '22 08:12

John Kugelman


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");
}
like image 41
Alistair Evans Avatar answered Dec 11 '22 09:12

Alistair Evans