0

So this has to be the easiest question of the day. I have below HTML structure where all input checkbox values must be unique.

    <tr>
        <td>
        <input type="checkbox" value="14292" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User One</td>
        <td>Autobot</td>
        <td>Shadow</td>
        <td>50</td>
    </tr>

 <tr>
        <td>
        <input type="checkbox" value="14293" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User One</td>
        <td>Autobot</td>
        <td>Billed</td>
        <td>50</td>
    </tr>


    <tr>
        <td>
        <input type="checkbox" value="14292" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User Two</td>
        <td>Autobot</td>
        <td>Billed</td>
        <td>50</td>
    </tr>

The value 14292 is generated twice here.

  1. How can I disable checkbox with duplicate value
  2. How can I disable checkbox with <td>Shadow</td>
3
  • On what condition you want to disable duplicate checkbox? Commented Apr 17, 2015 at 12:13
  • if duplicate checkbox values. But that has to be on attribute <td>Shadow</td> Commented Apr 17, 2015 at 12:15
  • @Slimshadddyyy I have edited my answer to fit your needs Commented Apr 17, 2015 at 12:26

1 Answer 1

3

If you want to add the disabled attribute to the next occurrences of checkboxes with the same value :

$('.chec_emp').each(function(){
    if ($(this).is(':enabled')) {
        $('.chec_emp[value="'+$(this).val()+'"]').not(this).prop('disabled', 'disabled');
    }
});

demo

Sign up to request clarification or add additional context in comments.

10 Comments

Brewal: The code works if the duplicate value is occuring next. But what if there is another checkbox between two checkboxes with same value ?
You can try it, but it should work. At each iteration, it looks for all other checkboxes with the same value. ("Other" beeing .not(this))
@Slimshadddyyy I updated the demo with your new html
Brewal: When I do select all to select all checkboxes, the disabled checkbox also displays selected Also one can change the disabled property from console and can again select it
What do you mean by select all ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.