HTML DOM isSameNode( ) Method

HTMLWeb DevelopmentFront End Technology

The HTML DOM isSameNode() method returns a boolean value (true/false) if two nodes are same or not.

Note − The isSameNode() method is deprecated so you should use ‘===’ operator instead.

Syntax

Following is the syntax −

Calling isSameNode()

nodeOne.isSameNode(NodeTwo)

Example

Let us see an example for isSameNode() method −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>isSameNode()</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
   ul{
      width: 30%;
      margin: 0 auto;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>isSameNode( )</legend>
<h3>John Doe's Achievements</h3>
<ul>
<li id="cricket">Trophy 1</li>
<li>Trophy 2</li>
<li>Trophy 3</li>
</ul>
<input type="button" onclick="confirmTrophy()" value="Check">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var cricketTrophy = document.getElementById("cricket");
   var trophyOne = document.getElementsByTagName("li")[0];
   divDisplay.textContent = 'NOTE: Check if Trophy 1 was won in cricket';
   function confirmTrophy() {
      if(cricketTrophy.isSameNode(trophyOne))
         divDisplay.textContent = 'Trophy 1 was indeed won in cricket';
      else
         divDisplay.textContent = 'Trophy 1 was not won in cricket';
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Check’ button −

After clicking ‘Check’ button −

raja
Updated on 30-Jul-2019 22:30:26

Advertisements