When manipulating the DOM with Javascript, try to avoid creating elements as much as possible. Instead, clone an HTML template, then use Javascript to alter the text and attributes of those elements.
The main benefit of this practice is to keep the HTML separate from the Javascript source. This way, you can style and rearrange HTML elements as much as you’d like, without having to modify your Javascript code.
For example, say we have a list of items grabbed from a feed:
<ul id="feeds">
<li>
<h2 class="title">Item title</h2>
<div class="description">
Lorem ipsum sin dolor sit amet...
</div>
</li>
...
</ul>
And say in your Javascript code (jQuery in this example), you’re dumping a feed’s items into this list:
$.get('/feed/url', function(results) {
for (var i = 0; i < results.entries.length; i++)
{
var entry = results.entry[i];
var li = $('<li></li>');
$(li).text(entry.title);
...
$(li).appendTo('#feeds');
}
});
Above, we’re using jQuery to create the <li> element. There’s really nothing wrong with this, but if you use an HTML template beforehand, you can save yourself some time in the future, when you need to make changes.
So our plan is to:
- Create an HTML template for the list item
clone()this with jQuery- Alter the attributes and text with jQuery
- Then append it to the
<ul>
So let’s start with the HTML template:
<ul id="feeds">
<li class="template" style="display:none">
<h2 class="title">Item title</h2>
<div class="description">
Lorem ipsum sin dolor sit amet...
</div>
</li>
</ul>
Now we have a simple HTML template for our list item. Next, we’ll modify the Javascript to clone the list item, then modify its contents and their attributes, rather than creating elements directly.
$.get('/feed/url', function(results) {
for (var i = 0; i < results.entries.length; i++)
{
var entry = results.entry[i];
// clone li.template
var li = $('#feeds li.template').clone();
$(li).removeAttr('style')
.removeClass('template');
// manipulate its contents
$(li).find('.title').text(entry.title);
$(li).find('.description').text(entry.description);
...
$(li).appendTo('#feeds');
}
});
Now you can modify the HTML, move things around, or change the elements without having to modify your Javascript. Good practice for future-proofing!
What do you think? Do you have a different opinion or other practices?









I think this is a great solution, and well described. Debugging errors in HTML/CSS is hard enough as it is (especially for novices — I’m a teacher) without having the stuff (DOM nodes resulting from the HTML/CSS) being created by jQuery. This way, they debug the template once, and the jQuery code just stuffs the actual data into the template. I think this is much better than having jQuery build the stuff.
Well done!
I am also thinking about this, but pros and cons. My thoughts:
Creating html element is not a bad thing:
– You can use jQuery function with attributes in order to assign text, event, css or any attribute to the element immediately, no need to use selector afterwards.
– You can save reference to the element after creation to further usage. E.g. fetching value from input or change something style.
– You don’t have to rely on dom structure, classes, id, just use your js reference.
– It’s also maintainable, because you can easily change structure, just append to another parent.
But drawbacks:
– Maybe it’s not that readable at the first look.
– Yes, you don’t have template for html, but do you need it for two elements? Almost everything is changeable via css, only place and order of elements in DOM not.
Question for templating modell:
– When do you download the template? On demand? It costs another request.
– How do you manage to handle animation based on template? For example if you put the menu to the right side not to the left. Some javascript animation should work differently.
I still don’t have the ultimate solution. Now I would prefer separating GUI and Data related JS as much as possible, and do the customisability stuff with JS inheritence pattern.
What do you think?