isSameNode

In this chapter you will learn:

  1. How to compare the results from two different queries
  2. How to compare two table rows

Compare the nodes

isSameNode method compares objects that you have obtained from different queries.

<!DOCTYPE HTML> <!--from   j  a v  a2s  .  c o m-->
<html> 
<body> 
    <table> 
        <tbody id="SurveysBody"> 
            <tr id="myRow"><td>A</td><td>B</td></tr> 
        </tbody> 
    </table> 
    <pre id="results"></pre> 
    <script> 
        var elemByID = document.getElementById("myRow"); 
        var elemByPos = 
          document.getElementById("SurveysBody").getElementsByTagName("tr")[0]; 
        if (elemByID.isSameNode(elemByPos)) { 
            document.getElementById("results").innerHTML = 
                                             "Objects are the same"; 
        } 
    </script> 
</body> 
</html>

Click to view the demo

Compare two table rows

The following code compares two rows in a table.

<!DOCTYPE HTML> <!--  j a v  a 2 s  .co  m-->
<html> 
    <body> 
        <table> 
            <tbody> 
                <tr class="myRow"><td>Plum</td><td>Purple</td></tr> 
            </tbody> 
        </table> 
        <table> 
            <tbody> 
                <tr class="myRow"><td>Plum</td><td>Purple</td></tr> 
            </tbody> 
        </table> 
        <pre id="results"></pre> 
        <script> 
            var elems = document.getElementsByClassName("myRow"); 
            if (elems[0].isEqualNode(elems[1])) { 
                document.getElementById("results").innerHTML = 
                                             "Elements are equal"; 
            } else { 
                document.getElementById("results").innerHTML = 
                                             "Elements are NOT equal"; 
            } 
        </script> 
    </body> 
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to access outer html
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