How to check two elements are same using jQuery/JavaScript ? Last Updated : 19 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an HTML document containing two elements and the task is to check whether both elements are same or not with the help of JavaScript. Approach 1: Use is() method to check both selected elements are same or not. It takes an element as argument and check if it is equal to the other element. Example: This example implements the above approach. html <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var id1 = "GFG_UP"; var id2 = "GFG_UP"; up.innerHTML = "Click on the button to check if " + "both elements are equal.<br>" + "id1 = " + id1 + "<br>id2 = " + id2; function GFG_Fun() { if ($('#GFG_UP').is($('#GFG_UP'))) { down.innerHTML = "Both elements are same"; } else { down.innerHTML = "Both elements are different"; } } </script> Output: Approach 2: The == operator is used to compare two JavaScript elements. If both elements are equal then it returns True otherwise returns False. Example: This example implements the above approach. html <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var id1 = "GFG_UP"; var id2 = "GFG_DOWN"; up.innerHTML = "Click on the button to check if both" + " elements are equal.<br>" + "id1 = " + id1 + "<br>id2 = " + id2; function GFG_Fun() { if ($('#GFG_UP')[0] == $('#GFG_DOWN')[0]) { down.innerHTML = "Both elements are same"; } else { down.innerHTML = "Both elements are different"; } } </script> Output: Comment More infoAdvertise with us Next Article How to attach a method to HTML element event using jQuery ? P PranchalKatiyar Follow Improve Article Tags : JavaScript JavaScript-Questions jQuery-Questions Similar Reads How to check two objects have same data using JavaScript ? In JavaScript, we cannot directly compare two objects by equality operators (double equals == or triple equals ===) to see whether they are equal or not. Comparing two objects like this results in false even if they have the same data. It is because those are two different object instances, they are 2 min read How to Check if an element is a child of a parent using JavaScript? In this article, we are going to see the methods by which we can Check if an element is a child of a parent using JavaScript. These are the following methods: Table of Content Using the Node.contains() methodLooping through the parents of the given childUsing the hasChildNodes() methodMethod 1: Usin 5 min read How to Check an Element with Specific ID Exists using JavaScript ? Given an HTML document containing some elements and the elements contain some id attribute. The task is to check whether the element with a specific ID exists or not using JavaScript. Below are the approaches to check an element with specific ID exists or not using JavaScript:Â Table of ContentApproa 3 min read How to attach a method to HTML element event using jQuery ? In this article, we are going to learn, how can we attach a method to an HTML element event using jQuery.There are various events like click(), mouseenter(), mouseout(), etc which are available in jQuery. For attaching a method to an HTML elements event, we will need a jQuery method on(), which will 2 min read How to compare two JavaScript array objects using jQuery/JavaScript ? In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using 3 min read How to check an array is empty or not using jQuery ? In this article, we will check if an array is empty or not using jQuery. In JavaScript, arrays are a special type of object. If we use the typeof operator for arrays, it returns "object". We can use jQuery's isEmptyObject() method to check whether the array is empty or contains elements. The isEmpty 2 min read Like