Seth
Seth

Reputation: 334

Disable checkboxes that contain different attribute

I am trying to disable checkboxes that have different warehouse locations once a warehouse is selected. For example, if I check California I want to disable all the checkboxes that are from other states. I am trying to do it based on the attribute whseID but I can't figure out how to have jquery make that distinction between that attribute. Sometimes I will ship multiple items from 1 warehouse. So when I check 1 checkbox in California I need the other one in California to remain enabled but I need Washington and Arizona disabled.

<table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr class="grdnt">
        <th style="color:black;">Checkbox</th>
        <th style="color:black;border-left:1px solid;">Warehouse</th>
        <th style="color:black;border-left:1px solid;">Item</th>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="CA" />
        </td>
        <td class="Whse">California</td>
        <td class="Item">J29458</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="CA" />
        </td>
        <td class="Whse">California</td>
        <td class="Item">J29478</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="WA" />
        </td>
        <td class="Whse">Washington</td>
        <td class="Item">J29478</td>
    </tr>
    <tr id="transferDetailCol">
        <td>
            <input type="checkbox" class="tfrCheck" name="tfrCheck[]" whseID="AZ" />
        </td>
        <td class="Whse">Arizona</td>
        <td class="Item">J29478</td>
    </tr>
</table>

$(document).on('click', '.tfrCheck', function () {
    var allCheckBox = $(".tfrCheck");
    var count_checked = allCheckBox.filter(":checked").length;
    var whseID = $(this).attr('whseID');
    if (count_checked >= 1) {

        $(".tfrCheck:contains(whseID)").attr('disabled', 'disabled');
        //$(".tfrCheck:not(:checked)").attr('disabled','disabled');

    } else {
        $(".tfrCheck").removeAttr('disabled');
    }
});

JSFIDDLE

Upvotes: 0

Views: 57

Answers (3)

Ragnar
Ragnar

Reputation: 4578

Try using this line:

$('.tfrCheck:not([whseID='+ whseID +'])').attr('disabled', 'disabled');

Upvotes: 2

Martin Denk
Martin Denk

Reputation: 554

The "attribute not equal selector" should help:

allCheckBox.filter('[whseID!="' + whseID + '"]').attr('disabled', true);

http://jsfiddle.net/oxkeL7jm/4/

Upvotes: 1

hair raisin
hair raisin

Reputation: 2628

Ragnar's answer is close, but since you want OTHER states to be disabled, use

$('.tfrCheck:not([whseID='+ whseID +'])').attr('disabled', 'disabled');

Upvotes: 3

Related Questions