Compare two nodes with isSameNode in JavaScript

Description

The following code shows how to compare two nodes with isSameNode.

Example


<!--from  w  w  w  .j ava2 s  .  com-->
<!DOCTYPE HTML>
<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

The code above generates the following result.

Compare two nodes with isSameNode in JavaScript
Home »
  Javascript Tutorial »
    DOM »
      Check
Javascript Tutorial Check
Check if an element has a certain attribute...
Check the length of a NodeList for a div el...
Check the length of the array returned from...
Compare two nodes with isSameNode in JavaSc...
Determine the node type in JavaScript
Find out the difference between innerHTML a...
Parse current document as the function para...