How to compare the results from two different queries in Javascript

Compare the nodes

isSameNode method compares objects that you have obtained from different queries.

Example


<!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"; 
        } <!-- w  w  w  .  ja  v  a 2s .  c  o m-->
    </script> 
</body> 
</html>

Click to view the demo

Compare two table rows

The following code compares two rows in a table.


<!DOCTYPE HTML> 
<html> 
    <body> 
        <table> 
            <tbody> 
                <tr class="myRow"><td>Plum</td><td>Purple</td></tr> 
            </tbody> 
        </table> 
        <table> 
            <tbody> 
                <tr class="myRow"><td>Plum</td><td>Purple</td></tr> 
            </tbody> 
        </table> 
        <pre id="results"></pre> 
        <script> 
            var elems = document.getElementsByClassName("myRow"); 
            if (elems[0].isEqualNode(elems[1])) { 
                document.getElementById("results").innerHTML = 
                                             "Elements are equal"; 
            } else { <!--   ww w  .j  a va 2 s  . c om-->
                document.getElementById("results").innerHTML = 
                                             "Elements are NOT equal"; 
            } 
        </script> 
    </body> 
</html>

Click to view the demo





















Home »
  Javascript »
    Javascript Reference »




Array
Canvas Context
CSSStyleDeclaration
CSSStyleSheet
Date
Document
Event
Global
History
HTMLElement
Input Element
Location
Math
Number
String
Window