isSameNode method
9 | ||||
Returns whether the current node is the same node as the specified one.
When we speak about nodes in JavaScript, it exactly means node object references.
Two node references are the same if they reference to the same object.
The strict equal operator (===) is equivalent to the isSameNode method for nodes in JavaScript.
There is a similar method to the isSameNode method in JavaScript, the isEqualNode method. It tests the equality of two node references. When two node references are the same then they are also equal, but different node references can also be equal. For detailed description, please see the page for the isEqualNode method.
If you need to compare the positions of two elements in the document hierarchy:
- In Internet Explorer before version 9, create TextRange objects from the elements first (see the createTextRange and moveToElementText methods) and use the compareEndPoints method on them.
- In other browsers, use the compareDocumentPosition.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. Reference to the node to compare. |
Return value:
Boolean. One of the following values:
The specified node is not the same as the current element. | |
The specified node is the same as the current element. |
Example HTML code 1:
This example illustrates the use of the isSameNode method:
|
||||
<head> <script type="text/javascript"> var lastButton = null; function Test (button) { if (lastButton) { if (button.isSameNode) { if (button.isSameNode (lastButton)) { alert ("Last time you clicked on the same button."); } else { alert ("You clicked on another button before."); } } else { alert ("Your browser does not support the isSameNode method."); } } lastButton = button; } </script> </head> <body> Click on the buttons! <button onclick="Test (this);">button1</button> <button onclick="Test (this);">button2</button> <button onclick="Test (this);">button3</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example implements a cross-browser solution for the previous example:
|
||||
<head> <script type="text/javascript"> var lastButton = null; function Test (button) { if (lastButton) { if (button === lastButton) { alert ("Last time you clicked on the same button."); } else { alert ("You clicked on another button before."); } } lastButton = button; } </script> </head> <body> Click on the buttons! <button onclick="Test (this);">button1</button> <button onclick="Test (this);">button2</button> <button onclick="Test (this);">button3</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
attribute, CommentNode, doctype, document, DocumentFragment, TextNode, XMLDocument
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
External links:
User Contributed Comments