Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1038136 of Node.isEqualNode()

  • Revision slug: Web/API/Node/isEqualNode
  • Revision title: Node.isEqualNode()
  • Revision id: 1038136
  • Created:
  • Creator: jsx
  • Is current revision? Yes
  • Comment touch

Revision Content

{{APIRef("DOM")}}

The Node.isEqualNode() method tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that much match varies depending on the types of the nodes.

Syntax

var isEqualNode = node.isEqualNode(otherNode);
  • otherNode: The node to compare equality with.

Example

In this example, we create three {{HTMLElement("div")}} blocks. The first and third have the same contents and attributes, while the second is different. Then we run some JavaScript to compare the nodes using isEqualNode() and output the results.

HTML

<div>This is the first element.</div>
<div>This is the second element.</div>
<div>This is the first element.</div>

<p id="output"></div>

JavaScript

let output = document.getElementById("output");
let divList  = document.getElementsByTagName("div");

output.innerHTML += "div 0 equals div 0: " + divList[0].isEqualNode(divList[0]) + "<br/>";
output.innerHTML += "div 0 equals div 1: " + divList[0].isEqualNode(divList[1]) + "<br/>";
output.innerHTML += "div 0 equals div 2: " + divList[0].isEqualNode(divList[2]) + "<br/>";

Results

{{ EmbedLiveSample('Example', 480) }}

Specifications

Specification Status Comment
{{SpecName('DOM WHATWG', '#dom-node-isequalnode', 'Node.isEqualNode')}} {{Spec2('DOM WHATWG')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.9")}} 9.0 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}

See also

  • {{domxref("Node.isSameNode()")}}

Revision Source

<div>
<div>{{APIRef("DOM")}}</div>
</div>

<p>The <code><strong>Node.isEqualNode()</strong></code>&nbsp;method&nbsp;tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that much match varies depending on the types of the nodes.</p>

<h2 id="Syntax" name="Syntax">Syntax</h2>

<pre class="syntaxbox">
var <var>isEqualNode</var> = <var>node</var>.isEqualNode(<var>otherNode</var>);
</pre>

<ul>
 <li><code>otherNode</code>: The node to compare equality with.</li>
</ul>

<h2 id="Example">Example</h2>

<p>In this example, we create three {{HTMLElement("div")}} blocks. The first and third have the same contents and attributes, while the second is different. Then we run some JavaScript to compare the nodes using <code>isEqualNode()</code> and output the results.</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">
&lt;div&gt;This is the first element.&lt;/div&gt;
&lt;div&gt;This is the second element.&lt;/div&gt;
&lt;div&gt;This is the first element.&lt;/div&gt;

&lt;p id="output"&gt;&lt;/div&gt;</pre>

<div class="hidden">
<h3 id="CSS">CSS</h3>

<pre class="brush: css">
#output {
&nbsp; width: 440px;
&nbsp; border: 2px solid black;
&nbsp; border-radius: 5px;
&nbsp; padding: 10px;
&nbsp; margin-top: 20px;
&nbsp; display: block;
}</pre>
</div>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">
let output = document.getElementById("output");
let divList  = document.getElementsByTagName("div");

output.innerHTML += "div 0 equals div 0: " + divList[0].isEqualNode(divList[0]) + "&lt;br/&gt;";
output.innerHTML += "div 0 equals div 1: " + divList[0].isEqualNode(divList[1]) + "&lt;br/&gt;";
output.innerHTML += "div 0 equals div 2: " + divList[0].isEqualNode(divList[2]) + "&lt;br/&gt;";</pre>

<h3 id="Results">Results</h3>

<p>{{ EmbedLiveSample('Example', 480) }}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-node-isequalnode', 'Node.isEqualNode')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1.9")}}</td>
   <td>9.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("Node.isSameNode()")}}</li>
</ul>
Revert to this revision