Patrick Reck Patrick Reck - 7 months ago 39
jQuery Question

Defining a HTML "template" to append using JQuery

I have got an array which I am looping through. Every time a condition is true, I want to append a copy of the

code below to a container element with some values.

Where can I put this HTML to re-use in a smart way?

<a href="#" class="list-group-item">
<td><img src=""></td>
<td><p class="list-group-item-text"></p></td>


$('.search').keyup(function() {

$.each(items, function(index) {



You could decide to make use of a templating engine in your project, such as:

If you don't want to include another library, John Resig offers a jQuery solution, similar to the one below.

Browsers and screen readers ignore unrecognized script types:

<script id="hidden-template" type="text/x-custom-template">

Using jQuery, adding rows based on the template would resemble:

var template = $('#hidden-template').html();

$('button.addRow').click(function() {