Is there a general practice for creating multiple complex HTML elements in jQuery? I tried several different ways.
At first I tried using createElement and linking a lot of them together with AppendTo and the like:
var badge = $(document.createElement("div")).attr("class", "wrapper1").appendTo("body"); $(document.createElement("div")).attr("class", "wrapper2").appendTo(".wrapper1"); $(document.createElement("table")).attr("class", "badgeBody").appendTo(".wrapper2"); $(document.createElement("tr")).attr("class", "row1").appendTo(".badgeBody"); $(document.createElement("td")).appendTo(".row1"); $(document.createElement("span")).attr("class", "badgeUnlocked").text("UNLOCKED! ").appendTo("td"); $(document.createElement("td")).attr("class", "badgeTitleText").appendTo(".row1"); $(document.createElement("span")).attr("class", "badgeTitle").text(name).appendTo(".badgeTitleText"); $(document.createElement("tr")).attr("class", "row2").appendTo(".badgeBody"); $(document.createElement("td")).appendTo(".row2"); $(document.createElement("img")).attr("src", imgUrl).appendTo(".row2 td"); $(document.createElement("td")).attr("class", "badgeText").appendTo(".row2"); $(document.createElement("span")).attr("class", "badgeDescription").text(description).appendTo(".badgeText");
This can be rude, because appendTo wants to add to each corresponding element so that everything needs its own name, otherwise it will be constantly added throughout the place.
Then I tried to create an array and merge it:
var badgeFragment = [ '<div><div id="'+ closeId+'" class="closeTab">X</div>', '<div id="'+ badgeId+'" class="wrapper1">', '<div class="wrapper2">', '<div class="badgeBody">', '<div class="badgeImage">', '<img src="'+ imgUrl +'">', '</div>', '<div class="badgeContents">', '<div class="badgeUnlocked">ACHIEVEMENT UNLOCKED: </div>', '<div class="badgeTitle">'+ name +'</div>', '<div id="'+ textId+'" class="badgeDescription">'+ description +'</div>', '</div>', '<div style="clear:both"></div>', '</div></div></div></div></div>', ] badgeFragment = $(badgeFragment.join(''));
This works very well, although in IE, when I put a warning ($ (badgeFragment) .text ()), it usually returned empty. (This was part of debugging a larger problem). I'm obviously a little new to jQuery (and even Javascript), so to try to make sure this is not a problem, I tried the third method - giant string concatenation:
var badgeFragment = '<div><div id="'+ closeId+'" class="closeTab">X</div>' + '<div id="'+ badgeId+'" class="wrapper1">' + '<div class="wrapper2">' + '<div class="badgeBody">' + '<div class="badgeImage">' + '<img src="C:/Users/Ryan/Documents/icons/crystal_project/64x64/apps/chat.png">' + '</div>' + '<div class="badgeContents">' + '<div class="badgeUnlocked">ACHIEVEMENT UNLOCKED: </div>' + '<div class="badgeTitle">test</div>' + '<div id="'+ textId+'" class="badgeDescription">test</div>' + '</div>' + '<div style="clear:both"></div>' + '</div></div></div></div></div>';
Is one of these methods better than the other? I'm not very good at various profilers, so I'm not sure how to check this. There is also the question of whether or not all of these methods are compatible with the browser.