Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery: find common elements in 2 sets of divs
    primarykey
    data
    text
    <p>For a markup like this:</p> <pre><code>&lt;div id="set1"&gt; &lt;div id="100"&gt;a div&lt;/div&gt; &lt;div id="101"&gt;another div&lt;/div&gt; &lt;div id="102"&gt;another div 2&lt;/div&gt; &lt;div id="120"&gt;same div&lt;/div&gt; &lt;/div&gt; &lt;div id="set2"&gt; &lt;div id="105"&gt;a different div&gt; &lt;div id="101"&gt;another div&lt;/div&gt; &lt;div id="110"&gt;more divs&lt;/div&gt; &lt;div id="120"&gt;same div&lt;/div&gt; &lt;/div&gt; </code></pre> <p>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?</p> <p>In other words after the script run the above code would look like this:</p> <pre><code>&lt;div id="set1"&gt; &lt;div id="100"&gt;a div&lt;/div&gt; &lt;div id="101" class="added"&gt;another div&lt;/div&gt; &lt;div id="102"&gt;another div 2&lt;/div&gt; &lt;div id="120" class="added"&gt;same div&lt;/div&gt; &lt;/div&gt; &lt;div id="set2"&gt; &lt;div id="105"&gt;a different div&gt; &lt;div id="101"&gt;another div&lt;/div&gt; &lt;div id="110"&gt;more divs&lt;/div&gt; &lt;div id="120"&gt;same div&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>EDIT</strong> playing around with it i did something but i am not sure it can go anywhere. I created an array with the ids in both sets and in Firebug i can see an array with the values</p> <pre><code> var arrEl = []; $('#set1 div, #set2 div').each( function(index) { var id = $(this).attr('id'); arrEl.push(id); //maybe somehow check the array for the values that appear twice, and add the class to the //matching divs? }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload