13

I have a form with 2 checkboxes and 2 sections of input text fields that are disabled.

If only the first checkbox is checked the following input text field should be enabled:

  • Email
  • Confirm Email
  • Full Name

If only the second checkbox is checked the following input text fields should be enabled:

  • Email
  • Confirm Email
  • Color
  • Size
  • Model

If both checkboxes are checked than all the input text fields should be enabled. The problem I have is that if both checkboxes are checked, then one is unchecked the 2 email text fields become disabled.

Here is a fiddle to what I have. I can use jQuery. http://jsfiddle.net/Ujxkq/

Here is my HTML:

<form id="myForm">
  <h3>Section 1</h3>
  Checkbox 1: <input type="checkbox" name="checkbox1" id="checkboxOne" onclick="enableDisableEmail(this.checked, 'emailAddr', 'emailConfirm');enableDisableSection1(this.checked, 'fullName');" />
  <p><input type="text" id="emailAddr" name="myEmailAddr" placeholder="Email" disabled /></p>
  <p><input type="text" id="emailConfirm" name="myEmailConfirm" placeholder="Confirm Email" disabled /></p>
  <p><input type="text" id="fullName" name="myFullName" placeholder="Full Name" disabled /></p>

  <h3>Section 2</h3>
  Checkbox 2: <input type="checkbox" name="checkbox2" id="checkboxTwo" onclick="enableDisableEmail(this.checked, 'emailAddr', 'emailConfirm');enableDisableSection2(this.checked, 'color', 'size', 'model');" />
  <p><input type="text" id="color" name="myColor" placeholder="Color" disabled /></p>
  <p><input type="text" id="size" name="mySize" placeholder="Size" disabled /></p>
  <p><input type="text" id="model" name="myModel" placeholder="Model" disabled /></p>
</form>

Here is my Javascript:

function enableDisableEmail(isEnabled, email, confirm) {
    document.getElementById(email).disabled = !isEnabled;
    document.getElementById(confirm).disabled = !isEnabled;
}

function enableDisableSection1(isEnabled, fullName) {
    document.getElementById(fullName).disabled = !isEnabled;
}

function enableDisableSection2(isEnabled, color, size, model) {
    document.getElementById(color).disabled = !isEnabled;
    document.getElementById(size).disabled = !isEnabled;
    document.getElementById(model).disabled = !isEnabled;
}

3 Answers 3

12

You tagged this question with jQuery, but you're not using it so far.
Here's a version that uses jQuery:

jQuery(function($) {
    $('#checkboxOne, #checkboxTwo').click(function() {
        var cb1 = $('#checkboxOne').is(':checked');
        var cb2 = $('#checkboxTwo').is(':checked');
        $('#emailAddr, #emailConfirm').prop('disabled', !(cb1 || cb2));
        $('#fullName').prop('disabled', !cb1);
        $('#color, #size, #model').prop('disabled', !cb2);    
    });
});

You can remove all onclick attributes on the checkboxes themselves if you're using this version.
The updated fiddle for this version is here: http://jsfiddle.net/tB7Yz/

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

5 Comments

Thanks. I did not use jQuery because I did not think it would be needed even though I do have it available. Sorry for tagging jQuery than if that was confusing.
It's not a problem. If you've got jQuery available and loaded on the page already, I personally prefer this version because it's more compact and removes application logic (onclick) from being embedded in the HTML.
Thanks jcsanyi. Would you mind explaining how the !(cb1 || cb2) evalutaes in the .prop() function? .prop('disabled', !(cb1 || cb2). I think I get that but I'm not sure that I do.
the .prop('disabled', !(cb1 || cb2)) is the same as the non-jQuery code .disabled = !(cb1 || cb2), except that it runs against both of the IDs referenced in the $() function. || means OR, and ! means NOT. so (cb1 || cb2) means (checkbox1 is checked, or checkbox 2 is checked). That evaluates to true if either checkbox is checked, or false otherwise. That's backwards from what we need for the disabled property, so we negate it with !.
Thanks jcsanyi. It's the negation, !, that I was not quite sure about. Thanks! :)
7

Instead of just toggling back and forth every time, check the status of both checkboxes, and decide whether each field should be enabled or disabled based on that.

You can simplify it down to a single function that gets called onclick of either checkbox:

function enableDisableAll() {
    var cb1 = document.getElementById('checkboxOne').checked;
    var cb2 = document.getElementById('checkboxTwo').checked;
    document.getElementById('emailAddr').disabled = !(cb1 || cb2);
    document.getElementById('emailConfirm').disabled = !(cb1 || cb2);
    document.getElementById('fullName').disabled = !cb1;
    document.getElementById('color').disabled = !cb2;
    document.getElementById('size').disabled = !cb2;
    document.getElementById('model').disabled = !cb2;
}

Updated fiddle is here: http://jsfiddle.net/p8gqZ/1/

1 Comment

Better because does not require use of Jquery
2

I changed a little your code an used Jquery

$(document).ready(function(){
var changeStatus = function(){
    var fne = $("#checkboxOne").prop("checked");
    $("#fullName").prop("disabled", !fne);

    var csme =$("#checkboxTwo").prop("checked");
    $("#color").prop("disabled", !csme);
    $("#size").prop("disabled", !csme);
    $("#model").prop("disabled", !csme);

    var any= fne || csme;
    $("#emailAddr").prop("disabled", !any);
    $("#emailConfirm").prop("disabled", !any);
};


$("#checkboxOne").on("change", changeStatus);
$("#checkboxTwo").on("change", changeStatus);

});

Fiddle

2 Comments

Thank you! I accepted the previous answer before I saw yours but thank you for showing me how to do it with jQuery.
@Mdd I believe you can change the accepted answer if you want. I don't think this is a good example of jQuery though - see my second answer for a better example of what's possible here with jQuery.

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.