I've found good posts on good practices inserting dom elements using jquery like this, this and this
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 ),
This is (just a small part) what I've got
<script type="text/javascript">
var phnFragment = "<tr><td align=\"right\">Telephone:</td><td colspan=\"2\"><select name=\"select\" size=\"1\"style=\"width: 100px;\"><option>-- Choose --</option><option>Home</option><option>Mobile</option><option>Work</option></select><input type=\"text\" size=\"20px\" /></td></tr>";
$(document).ready(function() {
$('#addmorephones').click(function() {
$("#phones").append(phnFragment);
});
});
</script>
....
....
<tr id="phones">
<td align="right">Telephone:</td>
<td colspan="2"><select name="select" size="1"
style="width: 100px;">
<option>-- Choose --</option>
<option>Home</option>
<option>Mobile</option>
<option>Work</option>
</select><input type="text" size="20px" /> <a href="javascript:void(0);" id="addmorephones">[+]</a>
</td>
</tr>
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?
Would love to learn alternatives