Can you make a "template" out of your string? If yes, then store it in a "constant" variable (e.g. defined in global scope), containing placeholders for actual variables, like {0}, {1} etc, as you would use it in C#'s string.format() method.
So, you would have code like this:
var rowTemplate = "<tr><td>{0}</td><td>SomePredefinedText {1}</td></tr>";
var alternateRowTemplate = "<tr><tdclass='a'>{0}</td><td>SomewhatDifferent {1}</td></tr>";
...... somewhere deep in your code
$("#someElement").append(
rowTemplate.format("myvalue1", "myvalue2")
).append(
alternateRowTemplate.format("myvalue3", "myvalue4")
);
var html = new $.stringBuilder();
for (var i in data)
html.cat("<tr><td>").cat(i).cat("</td><td></tr>");
$('#element').append(html.string());
The benefit of this approach is that you'll have fast concatenation (array joins perform better under IE6), and you could extend the object with other useful function (say, catIf that takes a boolean expression, or rep that repeats a given string several times).
Solution 4:
Or you could instead of concatenating strings together so make use of the array.join function.
Post a Comment for "Jquery Dynamically Create Table/tr/td Or Etc And Append Attributes"