Question

I want to apend a child ( <tr> ) to a table, after a specific row. because the rows get added by while mysqli_fetch_array()

I made a fiddle to show the problem. When you clic on add the text "abc" gets adedd. but behind the table, not as a new <tr> under the original row. Fiddle How can this be solved.

Was it helpful?

Solution

jsFiddle Demo

You are inserting a child of the table row with append Child. That is why the placement seems to be "not in the right place". It is however, right where you asked it to be placed. Instead, you can use insertBeforeMDN like this:

ct.parentNode.insertBefore(tr,ct.nextSibling);

This will look at the parent of the row (<tbody>) and then insert the new row before the next row of the selected button. Essentially this is "insertAfter", but that feature was never implemented for whatever reason.

As to the claim that insertBefore is somehow slow and should be avoided or that appendChild is preferred, it would seem that is not the case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top