jquery array: find elements that appear twice in the array

Posted by tsiger on Stack Overflow See other posts from Stack Overflow or by tsiger
Published on 2010-04-25T14:20:42Z Indexed on 2010/04/25 14:23 UTC
Read the original article Hit count: 252

Filed under:

For a markup like this:

<div id="set1">
 <div id="100">a div</div>
 <div id="101">another div</div>
 <div id="102">another div 2</div>
 <div id="120">same div</div>
</div>  

<div id="set2">
 <div id="105">a different div>
 <div id="101">another div</div>
 <div id="110">more divs</div>
 <div id="120">same div</div>
</div>

As you can see both #set1 and #set2 contain 2 divs with the same id (101, 120). Is it possible somehow with jQuery to find the common elements and add a class to the divs in #set1 that have the same id with divs in #set2?

In other words after the script run the above code would look like this:

<div id="set1">
 <div id="100">a div</div>
 <div id="101" class="added">another div</div>
 <div id="102">another div 2</div>
 <div id="120" class="added">same div</div>
</div>  

<div id="set2">
 <div id="105">a different div>
 <div id="101">another div</div>
 <div id="110">more divs</div>
 <div id="120">same div</div>
</div>

© Stack Overflow or respective owner

Related posts about jQuery