{{APIRef("DOM")}}
The Node.isSameNode()
method tests whether two nodes are the same, that is if they reference the same object.
Syntax
var isSameNode = node.isSameNode(other);
Parameters
other
The {{domxref("Node")}} to test against.
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 isSameNode()
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"></p>
CSS
#output { width: 440px; border: 2px solid black; border-radius: 5px; padding: 10px; margin-top: 20px; display: block; }
JavaScript
let output = document.getElementById("output"); let divList = document.getElementsByTagName("div"); output.innerHTML += "div 0 same as div 0: " + divList[0].isSameNode(divList[0]) + "<br/>"; output.innerHTML += "div 0 same as div 1: " + divList[0].isSameNode(divList[1]) + "<br/>"; output.innerHTML += "div 0 same as div 2: " + divList[0].isSameNode(divList[2]) + "<br/>";
Results
{{ EmbedLiveSample('Example', 480) }}
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', '#interface-node', 'Node')}} | {{Spec2('DOM WHATWG')}} | No change (was for a long time removed from it). |
{{SpecName('DOM3 Core', 'core.html#Node3-isSameNode', 'Node.isSameNode()')}} | {{Spec2('DOM3 Core')}} | Initial definition |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatGeckoDesktop("1.0")}} Removed in {{CompatGeckoDesktop("10")}} Readded in {{CompatGeckoDesktop("48")}} |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | {{CompatGeckoMobile("1.0")}} Removed in {{CompatGeckoMobile("10")}} Readded in {{CompatGeckoMobile("48")}} |
{{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
See also
- {{domxref("Node.isEqualNode()")}}