# How to check two elements are same using jQuery/JavaScript ?

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``>   ` ` `  `<``head``>  ` `    ``<``title``>  ` `       ``How to check two elements are same ` `       ``using jQuery/JavaScript? ` `    `` ` `     `  `    ``<``script` `src``= ` `"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"``> ` `    `` ` ` ` ` `  `<``body` `style` `= ``"text-align:center;"``>   ` `     `  `    ``<``h1` `style``=``"color:green;"``>   ` `        ``GeeksForGeeks   ` `    `` ` `     `  `    ``<``p` `id` `= ``"GFG_UP"` `style` `= ` `        ``"font-size: 15px; font-weight: bold;"``> ` `    `` ` `     `  `    ``<``button` `onclick` `= ``"GFG_Fun()"``> ` `        ``click here ` `    `` ` `     `  `    ``<``p` `id` `= ``"GFG_DOWN"` `style` `= ` `        ``"color:green; font-size: 20px; font-weight: bold;"``> ` `    `` ` `     `  `    ``<``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"; ` `            ``} ` `        ``} ` `    ``  ` `   ` ` `  ` `

Output:

• Before clicking on the button: • After clicking on the button: 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 implaments the above approach.

 `   ` `<``html``>   ` ` `  `<``head``>  ` `    ``<``title``>  ` `        ``How to check two elements are same ` `        ``using jQuery/JavaScript ? ` `    `` ` `     `  `    ``<``script` `src``= ` `"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"``> ` `    `` ` ` ` ` `  `<``body` `style` `= ``"text-align:center;"``> ` `     `  `    ``<``h1` `style``=``"color:green;"``>   ` `        ``GeeksForGeeks   ` `    `` ` `     `  `    ``<``p` `id` `= ``"GFG_UP"` `style` `= ` `        ``"font-size: 15px; font-weight: bold;"``> ` `    `` ` `     `  `    ``<``button` `onclick` `= ``"GFG_Fun()"``> ` `        ``click here ` `    `` ` `     `  `    ``<``p` `id` `= ``"GFG_DOWN"` `style` `= ` `        ``"color:green; font-size: 20px; font-weight: bold;"``> ` `    `` ` `     `  `    ``<``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') == \$('#GFG_DOWN')) { ` `                ``down.innerHTML = "Both elements are same"; ` `            ``} else {     ` `                ``down.innerHTML = "Both elements are different"; ` `            ``} ` `        ``} ` `    ``  ` `   ` ` `  ` `

Output:

• Before clicking on the button: • After clicking on the button: My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5002676,"math_prob":0.7565519,"size":3549,"snap":"2020-24-2020-29","text_gpt3_token_len":1015,"char_repetition_ratio":0.1393512,"word_repetition_ratio":0.52615386,"special_character_ratio":0.31783602,"punctuation_ratio":0.14802632,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96065724,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,3,null,3,null,3,null,3,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-30T19:42:31Z\",\"WARC-Record-ID\":\"<urn:uuid:550df44d-a888-4634-9dba-18e50ec00eb9>\",\"Content-Length\":\"147239\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2ecae222-8d91-4bed-9328-266a24de0baa>\",\"WARC-Concurrent-To\":\"<urn:uuid:a340f976-08a6-41fe-a158-f5d18a4f0d69>\",\"WARC-IP-Address\":\"23.199.63.170\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/how-to-check-two-elements-are-same-using-jquery-javascript/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:KGIO2PB7VGZDQUJ6QXTZM74FIQEZYQEH\",\"WARC-Block-Digest\":\"sha1:HMF3TQARREJNUJOS2QRVPG4I4P73FLIL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347410284.51_warc_CC-MAIN-20200530165307-20200530195307-00195.warc.gz\"}"}