5

How do you combine a radio and checkbox input? With that I mean when you select the radio button it deselects the checkboxes and when you select a checkbox the radio input gets deselected.

Here's my example input form:

<input type="checkbox" name="d[]"> option 1<br/>
<input type="checkbox" name="d[]"> option 2<br/>
<input type="checkbox" name="d[]"> option 3<br/>
<input type="radio" name="d[]"> option 4<br/>

Which would look like this

Example render of code.

Now if I were to select option 4 I want option 2 and option 3 to deselect. How do I as best do this? Is there some easy trick or do I have to include some javascript magic?

4
  • 2
    You would need JavaScript. It's not a good idea for reasons of usability. Having things behave in a way that users do not expect violates the Principle of Least Astonishment: en.wikipedia.org/wiki/Principle_of_least_astonishment Commented Oct 5, 2014 at 20:09
  • @BobBrown But it would act as I expect 'cause in my mind radio buttons deselect when you select anything else from the same group. And that is what I want to acomplish here. Commented Oct 5, 2014 at 20:11
  • It may act as you expect, but it won't act as your readers expect. You do not have radio buttons. You have a combination of checkboxes and radio buttons. Users will not expect that operating a radio button will change any checkbox. Suggestion: Use all checkboxes and eliminate invalid combinations with JavaScript and an explanatory message. Commented Oct 5, 2014 at 20:16
  • @BobBrown So you're saying I should change system? And not use a custom system? I could do that but I thought it would be nice. Commented Oct 5, 2014 at 20:21

3 Answers 3

3

Check the following (JQuery is required). Please also read my comment on OP's accepted answer by @Maxim Orlovsky)

$('input[name="d[]"]').on('change', function(){
    if ($(this).attr('type') == 'radio' ) {
        if ( $(this).prop('checked') ) {
            $('input[name="d[]"][type="checkbox"]').prop('checked', false);
        }
    }
    else {
        if ( $(this).prop('checked') ) {
            $('input[name="d[]"][type="radio"]').prop('checked', false);
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="d[]"> option 1<br/>
<input type="checkbox" name="d[]"> option 2<br/>
<input type="checkbox" name="d[]"> option 3<br/>
<input type="radio" name="d[]"> option 4<br/>

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

1 Comment

IMO the MaximOrlovsky's answer is bugged (if you select option 2, then option 3 and then deselect option 2, the option 4 is automatically selected by itself). However ira's solution is the best answer to akaJag's question.
2

You need some JavaScript (jQuery):

$('input[name="d[]"]').on('change', function(){
    if ($(this).attr('type') == 'radio' ) {
        if ( $(this).prop('checked') ) {
            $('input[name="d[]"][type="checkbox"]').prop('checked', false);
        }else {
            $('input[name="d[]"][type="checkbox"]').prop('checked', true);
        }
    }
    else {
        if ( $(this).prop('checked') ) {
            $('input[name="d[]"][type="radio"]').prop('checked', false);
        }else {
            $('input[name="d[]"][type="radio"]').prop('checked', true);
        }
    }
});

Live demo -- http://jsfiddle.net/u2n8j5r2/1/

5 Comments

That's actually jQuery, so OP would need to include jQuery in his page. Also, with your fiddle, I can pick Option 4, then go back and select options 2 and 3.
This doesn't deselect the radio button when I select a checkbox. Sorry for being unclear and thanks for quick respond.
@akaJag : I updated my code, now it works fine -- jsfiddle.net/u2n8j5r2/1.
@MaximOrlovsky Works nicely! Thanks again for quick respond! I am not certain though if I should use this now concidering it may confuse some users.
@Maxim Orlovsky Please do check your code again... Looks like something you are missing. For example, if you select option 2, then option 3 and then deselect option 2, the option 4 is automatically selected by itself.
1

Here is some code that uses all checkboxes, does not require jQuery, and should do what you want. (I've edited the code to change when the error message is removed.)

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Checkboxes</title>
<style type="text/css">
</style>
<script type="text/javascript">
//<![CDATA[
function chkBoxes() {
    var msg = document.getElementById("msg");
        cb1 = document.getElementById("cb1"),
        cb2 = document.getElementById("cb2"),
        cb3 = document.getElementById("cb3"),
        cb4 = document.getElementById("cb4");

    if (cb4.checked) {
        if (cb1.checked || cb2.checked || cb3.checked) {
            msg.innerHTML = "Option 4 not allowed with options 1-3";
            cb1.checked = false;
            cb2.checked = false;
            cb3.checked = false;
        } 
    } else {
        msg.innerHTML = "";
    }
}
//]]>
</script>
</head>
<body>
<input type="checkbox" name="d[]" id="cb1" value="1" onchange="chkBoxes();"> option 1<br/>
<input type="checkbox" name="d[]" id="cb2" value="2" onchange="chkBoxes();"> option 2<br/>
<input type="checkbox" name="d[]" id="cb3" value="3" onchange="chkBoxes();"> option 3<br/>
<input type="checkbox" name="d[]" id="cb4" value="4" onchange="chkBoxes();"> option 4<br/>
<p id="msg"></p>
</body>
</html>

1 Comment

Ok yea I like your answer but I like Maxim's version more 'cause it works if they got the same name. And I think yours makes it so you can't select checkbox 1-3 when 4 is selected. I wanted the 4th option to be a radio just so you can't unselect it except by selecting checkbox 1-3

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.