views:

37

answers:

3

I am trying to clone a template of a which will be populated with data and then inserted into a table. I'm currently able to use the same .clone() function with other elements on the same page but jQuery refuses to see and clone the template (I'm guessing because its not a block element).

Here is the template:

<tr id="search_result_temp" class="template">
<td class="logo">
    <img class="logo" />
</td>
<td class="ratings">
    <p id="rating"></p>
</td>
<td class="programs"></td>
<td class="actions">
    <button id="request_info">Request Info</button>
    <button id="save_school">Save</button>
</td>
<td class="compare"></td>
</tr>

Here is the javascript code:

for(var index in results){
        var result = results[index];
        $result_listing = $("#search_result_temp").clone().removeClass('template').attr('id', result.key);
        $search_results.append($result_listing);
    }

Thanks!

A: 
for(var index in results){
        var result = results[index];
        $result_listing = $("#search_result_temp").clone().removeClass('template').attr('id', result.key);
        $search_results.append($result_listing);
    }

correct me if I'm wrong, but why do you need to

var result = results[index];

index is already a single element of results

drachenstern
Not in javascript, index is just the index(label)
Dr.Molle
@Dr.Molle ... no kidding, it doesn't work like other languages collections? How interesting. Then why not `for(i=0;i<results.count;i++)` ? Or is that the shorthand version of?
drachenstern
In javascript the for loop iterates through the 'keys' of the object. So index refers to the key not the value. @drachenstern - Those loops have the same effect
Justin Lucas
Its really not the same. use `for(i=0;i<arrArray.length;i++)` for arrays, and `for(var index in objObject)` for all objects except arrays. The second way could also return other members if used onto an array, for example **length**, that's the hook.
Dr.Molle
Example: `Array.prototype.foo='bar';var arr=[1,2,3];for(var k in arr){alert(k);}`
Dr.Molle
@Dr.Molle - That difference is good to know. Thanks. 'results' is an object literal as opposed to an array.
Justin Lucas
@Dr.Molle ~ Thanks!
drachenstern
+1  A: 

Found the answer. The clone() function works fine when the template <tr> is encapsulated within <table> and <div> elements. As stated below .innerHTML (which jQuery uses in clone()) does not work with table fragments. That means jQuery will not clone a <tr> if it is a root element (whose parent element is <body>). Therefore, the template should look like this(of course with an appropriate id on the <div> to be selected by jQuery):

<div>
<table>
<tr id="search_result_temp" class="template">
<td class="logo">
    <img class="logo" />
</td>
<td class="ratings">
    <p id="rating"></p>
</td>
<td class="programs"></td>
<td class="actions">
    <button id="request_info">Request Info</button>
    <button id="save_school">Save</button>
</td>
<td class="compare"></td>
</tr>
</table>
</div>
Justin Lucas
If you want to know why you have to do it this way, it is due to browser limitations in setting table fragments with `.innerHTML`
staticsan
A: 

Alternatively you can wrap the template in a hidden DIV and access the content via jQueries HTML method. On a side note, you might bump heads cloning several items with the same ID. Try use a class approach and add an identifier (Try the new HMTL5 data-* tag attribute).

Slappy