1

I found found several question's answers similar to this, I tried some of them, but no luck. May my scenario is different, I want to make other checkbox disable when anyone of them is clicked. if one is clicked second go disabled.

Edit:

I have two checkboxes i.e; chk1 and chk2. if chk1 is checked chk2 become disable, if chk2 is checked chk1 become disable

HTML

<tr class="cart-item-row">
<td class="remove-from-cart">
<label class="td-title">Remove:</label>
     <input type="checkbox" name="removefromcart" value="4392" tabindex="15" class="class2">
</td>
<td class="add-to-cart">
<label class="td-title">Buy Now:</label>
    <input type="checkbox" name="addtocart" value="4392" tabindex="16" class="class2">
</td>                                                                                                                      

 </tr>

JS

<script>
    $(function () { // I am also adding class to checkbox through JS
        $('.cart-item-row > td > input[type="checkbox"]').addClass("class2");
    });
</script>
<script>
    //$('input[class^="class"]').click(function () {
 //$('.class2').unbind().click(function () {
    $('.class2').click(function () {
        var $this = $(this);

        if ($this.is(".class2")) {
            if ($this.is(":checked")) {
                $(".class2").not($this).prop({ disabled: true, checked: false });

            } else {
                $(".class2").prop("disabled", false);
            }
        }
    });
</script>
8
  • It sounds like you want a radio input type instead of a checkbox, that way it's default browser behaviour :) Commented Sep 2, 2016 at 6:50
  • @jedifans but I want to perform this with checkboxes Commented Sep 2, 2016 at 6:50
  • can you please re write the requirement. The code you wrote is different from the problem description Commented Sep 2, 2016 at 6:51
  • @qadeer but why? Do you have more than two options? Commented Sep 2, 2016 at 6:51
  • @user2181397 edited Commented Sep 2, 2016 at 6:54

2 Answers 2

1

You have missing , You can place your jquery within

$(document).ready(function(){ }

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

Comments

1

You have to put your click event in document ready state $(function () { });

You can see here working Example

Comments

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.