I’ve been working on an app and I noticed I tend to create DOM objects in JavaScript/jQuery and then doing appendTo(‘body’) like so:
$list = $('<ul/>', {
id : "thelist",
class : "listclass"
}
$list.appendTo('body');
$list.html(whateverAjaxStuff);
and my page source ends up just being a head with an empty body tag.
I’m just wondering if there are any advantages/disadvantages to this method. Should I be building the basic framework in HTML then just manipulate the innerHTML with JavaScript instead? Thoughts?
EDIT: Wanted to clarify. I’m only doing this with stuff that has to be generated dynamically. $list elements are loaded with AJAX, the user makes a selection which populates another list, etc.
I don’t think the other answers above really persuade enough as to why you definitely shouldn’t do what you are doing now. There are so many reasons not to do this;
I can’t think of any reason to construct all of your page using jQuery, you appear to be attempting to emulate what happens server-side (Ruby, ASP.net, PHP etc)
You may want to edit the DOM from JS.. but constructing it entirely from scratch is a very bad idea!