Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery:Inserting big amounts of html through DOM manipulation - maintainability?
    primarykey
    data
    text
    <p>I've found good posts on good practices inserting dom elements using jquery like <a href="https://stackoverflow.com/questions/117665/jquery-fastest-dom-insertion">this</a>, <a href="https://stackoverflow.com/questions/3667469/javascript-jquery-best-way-to-insert-a-big-piece-of-code-into-dom">this</a> and <a href="https://stackoverflow.com/questions/117665/jquery-fastest-dom-insertion/4421104#4421104">this</a></p> <p>It's ok when you just need to insert a few elements, but it seems to me that these option are very bad from the point of view of maintainability (Code is repeated twice, thus if any change is needed it needs to happen twice then it becomes very prone to error ), </p> <p>This is (just a small part) what I've got</p> <pre><code>&lt;script type="text/javascript"&gt; var phnFragment = "&lt;tr&gt;&lt;td align=\"right\"&gt;Telephone:&lt;/td&gt;&lt;td colspan=\"2\"&gt;&lt;select name=\"select\" size=\"1\"style=\"width: 100px;\"&gt;&lt;option&gt;-- Choose --&lt;/option&gt;&lt;option&gt;Home&lt;/option&gt;&lt;option&gt;Mobile&lt;/option&gt;&lt;option&gt;Work&lt;/option&gt;&lt;/select&gt;&lt;input type=\"text\" size=\"20px\" /&gt;&lt;/td&gt;&lt;/tr&gt;"; $(document).ready(function() { $('#addmorephones').click(function() { $("#phones").append(phnFragment); }); }); &lt;/script&gt; .... .... &lt;tr id="phones"&gt; &lt;td align="right"&gt;Telephone:&lt;/td&gt; &lt;td colspan="2"&gt;&lt;select name="select" size="1" style="width: 100px;"&gt; &lt;option&gt;-- Choose --&lt;/option&gt; &lt;option&gt;Home&lt;/option&gt; &lt;option&gt;Mobile&lt;/option&gt; &lt;option&gt;Work&lt;/option&gt; &lt;/select&gt;&lt;input type="text" size="20px" /&gt; &lt;a href="javascript:void(0);" id="addmorephones"&gt;[+]&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>Is there a better way to accomplish this? Is there a way to tell jquery to copy part of the dom tree and use it as fragment to insert?</p> <p>Would love to learn alternatives</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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