Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery with uncomplete DOM elements
    text
    copied!<p>My problem is related to jQuery and the DOM elements. I need a template like the following:</p> <pre><code>var threadreply = " &lt;li class='replyItem'&gt;" + " &lt;div class='clearfix'&gt;" + " ${tittle}" + " &lt;/div&gt;" + " &lt;/li&gt;" ; $.template( "threadreply", threadreply ); </code></pre> <p>As you can see, this is a list element. My problem is when I parse it with <code>$.tmpl</code>, which retrieves a valid DOM element without the <code>&lt;li&gt; &lt;/li&gt;</code> tags.</p> <pre><code>liElement = liElement + $.tmpl("threadreply", {"tittle": "hello"} ).html(); </code></pre> <p>Is there any way I can retrieve the element without reformatting?</p> <p>I know I can do it with a template with a valid <code>ul</code> tag and inside an each jQuery template loop, but I'm not working with JSONs, I can't convert my data structures to JSON.</p> <p>The full example is as follow:</p> <pre><code>var threadreply = " &lt;li class='replyItem'&gt;" + " &lt;div class='clearfix'&gt;" + " ${tittle}" + " &lt;/div&gt;" + " &lt;/li&gt;" ; $.template( "threadreply", threadreply ); var liElement = ""; for( var i = 0; i &lt; 150; i ++ ){ liElement = liElement + $.tmpl("threadreply", {"tittle": "hello"} ).html(); } $(liElement).appendTo("#ULElement"); </code></pre> <p>EDITED</p> <p>I found a workaround with this thread: <a href="https://stackoverflow.com/questions/652763/jquery-object-to-string">JQuery Object to String</a> wich consists on wraping each DOM element returned by the <code>$.tmpl</code> in a <code>div</code> before get the <code>.html()</code> of the object:</p> <pre><code>liElement = liElement + $('&lt;div&gt;').append( $.tmpl("threadreply", {"tittle": "hello"} )).html(); </code></pre> <p>With 300 elements it takes aprox 290ms in process all elements. With the <code>appendTo()</code> inside the loop, it takes more than 800ms.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload