Let’s say I have the following table:
<table>
<tr id="1">...</tr>
<tr id="2">..</tr>
...
<tr id="last">..</tr>
</table>
I also have a third-party service from which I get some raw html, also table rows like this:
<tr id="additional-1">...</tr>
<tr id="additional2">...</tr>
Is there a relatively simple javascript way to insert those new rows after the tr with the id “last”?
I’m asking for simple built-in ways to avoid having to do a lot of parsing, replacing and stuff.
I prefer YUI 3 to jQuery solution.
Wrap the string in
<table><tbody>..rows here...</tbody></table>. Then, create a dummy element, eg<div>, and set theinnerHTMLproperty to the previously constructed string. Finally, loop through all rows, and move the rows to the table usinginsertBefore(newelem, reference).This method also works in IE, where setting the
innerHTMLproperty on a cell triggers an error.