1

I have created a dropdown and few checkboxes. checkboxes enable and disable based on dropdown option selected.

Here is the code:

<select id="Objective" form="theForm" name="category" >
    <option value="pi">Pipi</option>
    <option value="theta">thethaatha</option>
    <option value="sigma">sigmama</option>
</select>
<div>
    <input type="checkbox" class="dissable" onclick="check();" value="1" /> Teas<br>
    <input type="checkbox" class="dissable" onclick="check();" value="1" /> Capri<br>
    <input type="checkbox" class="dissable" onclick="check();" value="2" /> Kids <br>
    <input type="checkbox" class="dissable" onclick="check();" value="2" /> Food <br>
</div>

<script>
$("#Objective").on("change", function () {
    if ($(this).val() == "theta") {
        $(".dissable[value='1']").prop("disabled", true);
    } else if ($(this).val() == "pi") {
        $(".dissable[value='2']").prop("disabled", true);
    } else {
        //donothing
    }
}).trigger('change');
</script>

whenever I choose pi from dropdown it goes fine both value = "2" kids and food gets disabled but when i choose theta from every checkbox gets disabled which should not be like this. only value ="1" should get disabled i.e., teas and capri.

Help correcting it whats the problem with code. thanks in advance.

1

2 Answers 2

1

An easy way to do it, is enabling all checkboxes before disabling choosed ones, in order to ensure that only those you want are disabled;

// enable all checkboxes
$(".dissable").prop("disabled", false);

and then you can disable choosed ones:

// disabling some of them
$(".dissable[value='1']").prop("disabled", true);

Working example: http://codepen.io/anon/pen/bwJaEp?editors=1010

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

1 Comment

plus 1 from me, I should have thought of removing the attribute when resetting the property
1

You need to undo between each select change so the disabled one's get enabled again, so if you add these 2 lines in the function beginning it will work

    $(".dissable[value='1']").prop("disabled", true);
    $(".dissable[value='2']").prop("disabled", true);

Side note, as "Ezequias Dinella" shows in his answer, to enable/disable all you can also remove the attribute, like this $(".dissable").prop("disabled", true);

Sample

$("#Objective").on("change", function () {
    $(".dissable[value='1']").prop("disabled", false);
    $(".dissable[value='2']").prop("disabled", false);
  
    if ($(this).val() == "theta") {
        $(".dissable[value='1']").prop("disabled", true);
    } else if ($(this).val() == "pi") {
        $(".dissable[value='2']").prop("disabled", true);
    } else {
        //donothing
        $(".dissable[value='1']").prop("disabled", true);
        $(".dissable[value='2']").prop("disabled", true);
    }
}).trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="Objective" form="theForm" name="category" >
    <option value="pi">Pipi</option>
    <option value="theta">thethaatha</option>
    <option value="sigma">sigmama</option>
</select>
<div>
<input type="checkbox" class="dissable" onclick="check();" value="1" /> Teas<br>
<input type="checkbox" class="dissable" onclick="check();" value="1" /> Capri<br>
<input type="checkbox" class="dissable" onclick="check();" value="2" /> Kids <br>
<input type="checkbox" class="dissable" onclick="check();" value="2" /> Food <br>
</div>

7 Comments

hey it worked..programming is fun everytime. thanks a ton
@SanchitGupta It is fun :)
there is on problem that is still there
whenever user chooses sigmama all checkboxes should get disabled..this doesn't work as expected
@SanchitGupta That was unclear, is it says donothing in the script. Will update my answer in a sec.
|

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.