NateNate NateNate - 4 months ago 8
jQuery Question

How to check for duplicates of select element inside a td?

From the question and demo,

How to compare several td.text() in tr using jQuery

https://jsfiddle.net/lmgonzalves/cqa6m6va/1/

it showed how to determine duplicates within td elements.

My problem is on how to do it within select elements inside a td:

<td style="max-width:160px" class="ellipsis">
<select class="select make-long-select fieldEditable ddSample" style="display:none;" name="sampleName">
<option value="SampleValue1">SampleValue1</option>
</select>
<span class="lblSample lblCurrentValue">SampleValue1</span>
</td>

<td style="max-width:160px" class="ellipsis">
<select class="select make-long-select fieldEditable ddSample" style="display:none;" name="sampleName">
<option value="SampleValue2">SampleValue2</option>
</select>
<span class="lblSample lblCurrentValue">SampleValue2</span>
</td>

<td style="max-width:160px" class="ellipsis">
<select class="select make-long-select fieldEditable ddSample" style="display:none;" name="sampleName">
<option value="SampleValue1">SampleValue1</option>
</select>
<span class="lblSample lblCurrentValue">SampleValue1</span>
</td>

<td style="max-width:160px" class="ellipsis">
<select class="select make-long-select fieldEditable ddSample" style="display:none;" name="sampleName">
<option value="SampleValue3">SampleValue3</option>
</select>
<span class="lblSample lblCurrentValue">SampleValue3</span>
</td>


I need to implement the same behavior as the last question to this script.

I tried this approach but no luck:

tds = $(this).find('td.ddSample');
tds.each(function(j, elem1){
tds.each(function(k, elem2){
if($(elem1)[0] != $(elem2)[0] && $(elem1).text() == $(elem2).text()){
$(elem1).addClass('cl');
}
});
});

Answer

javascript

var tds;
$('tr').each(function(i, item){
    tds = $(this).find('td');
    tds.each(function(j, elem1){
        tds.each(function(k, elem2){     

            if($(elem1)[0] != $(elem2)[0] && $(elem1).find("select").html() == $(elem2).find("select").html()){
            console.log($(elem1).find("select").html());
                $(elem1).addClass('cl');
            }
        });
    });
});

jsfiddle.net

Comments