appendChild

In this chapter you will learn:

  1. How to add a child tag to a HTML element

Add a child

The following code adds a click event listener to a button. When you click the button it adds table row to SurveysBody table.

<!DOCTYPE HTML> <!--from  jav  a 2  s  .  c o m-->
<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

Next chapter...

What you will learn in the next chapter:

  1. How to use attributes property for html element
Home » Javascript Tutorial » HTML Operation
HTMLElement
addEventListener
appendChild
attributes
classList
className
cloneNode
createElement
createTextNode
dataset
getAttribute
getElementsByTagName
hasAttribute
innerHTML
insertAdjacentHTML
insertBefore
isSameNode
outerHTML
onmouseout
onmouseover
removeEventListener