Add child to a HTML element in JavaScript

Description

The following code shows how to add child to a HTML element.

Example


<!--  w  w  w .  jav a2s .  co  m-->

<!DOCTYPE HTML>
<html>
<body>
<table border="1">
<tbody>
<tr><td>A</td><td>B</td></tr>
<tr id="apple"><td>C</td><td>D</td></tr>
</tbody>
</table>
<br/>
<table border="1">
<tbody id="SurveysBody">
<tr><td>E</td><td>F</td></tr>
</tbody>
</table>
<p>
<button id="move">Move Row</button>
</p>
<script>
document.getElementById("move").onclick = function() {
var elem = document.getElementById("apple");
document.getElementById("SurveysBody").appendChild(elem);
};
</script>
</body>
</html>

Click to view the demo

The code above generates the following result.

Add child to a HTML element in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Insert
Javascript Tutorial Insert
Add child to a HTML element in JavaScript
Insert a node before another element using ...
Insert as the new first child in JavaScript
Insert before last child in JavaScript
Insert element before another element in Ja...
Insert to adjacent html with afterbegin in ...
Insert to adjacent html with afterend in Ja...
Insert to adjacent html with beforebegin in...
Insert to adjacent html with beforeend in J...