archytect
archytect

Reputation: 3725

removing duplicate checkboxes from a form

I have a form that's generated and sent back by mvc and razor, how would I remove the dupicates from the form? eg. there are two checkboxes with grapes and one with bananas, I'd wanna remove the duplicate grapes one. I could put $('.fruit') into an array, but how would I target the duplicates from there?

 <div class="fruit">
      <input data-val="true" data-val-number="" data-val-required="" name=" [5].UniqueValue[3].Id" type="hidden" value="123">
      <input name="[5].UniqueValue[3].fruit" type="hidden" value=".grapes">
      <input data-val="" data-val-required="" name="[5].UniqueValue[3].IsSelected" type="checkbox" value="true"><input name="[5].UniqueValue[3].IsSelected" type="hidden" value="false"> grapes
 </div>
 <div class="fruit">
      <input data-val="true" data-val-number="" data-val-required="" name="[5].UniqueValue[2].Id" type="hidden" value="321">
      <input name="[5].UniqueValue[2].fruit" type="hidden" value=".bananas">
      <input data-val="" data-val-required="" name="[5].UniqueValue[2].IsSelected" type="checkbox" value="true"><input name="[5].UniqueValue[2].IsSelected" type="hidden" value="false"> bananas
 </div>
 <div class="fruit">
      <input data-val="true" data-val-number="" data-val-required="" name="[5].UniqueValue[1].Id" type="hidden" value="456">
      <input name="[5].UniqueValue[1].fruit" type="hidden" value=".grapes">
      <input data-val="" data-val-required="" name="[5].UniqueValue[1].IsSelected" type="checkbox" value="true"><input name="[5].UniqueValue[1].IsSelected" type="hidden" value="false"> grapes
 </div>

Upvotes: 1

Views: 1332

Answers (1)

plalx
plalx

Reputation: 43748

You could use the :gt() and the :has() selectors.

FIDDLE

$('.fruit > [name$=fruit]').each(function () {
    $('.fruit:has([value="' + $(this).prop('value') + '"]):gt(0)').remove();
});

Upvotes: 2

Related Questions