1

I have a code where I expect my checkboxes to be selected and disabled. When I click Zero, all checkboxes should be highlighted, all checkboxes except zeroth checkbox should be disabled. Similarly for one, two and three radio buttons. This does not seem to happen consistently. I am trying it on chrome browser version 48.0.2564.116. Also, the behavior is horrible on Firefox. Can someone please let me know what I am doing wrong?

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script>
      $(document).ready(function(){
        $("input[name=radio_group]").prop("checked", false);
        $("input[type=radio]").click( function( e ){
           var whats_selected = $("input[name=radio_group]:checked").val()
           $("input[type=checkbox]").attr('checked',false );

         //disable all other checkboxes
         for(i=0; i < 4; i++ ){
           var elem = $("input[type=checkbox][name*=checkbox"+i+"]");
           elem.click();
           if( i != whats_selected ){
             elem.prop("disabled", true);
           }else{
             elem.removeAttr("disabled");
           }
         }
        });
      });
    </script>
  </head>
  <body>
    <h1>Checkbox play</h1>
    <h3>My 4 Radio Buttons</h3>

    <input type="radio" name='radio_group' value=0>Zero<br>
    <input type="radio" name='radio_group' value=1>One<br>
    <input type="radio" name='radio_group' value=2>Two<br>
    <input type="radio" name='radio_group' value=3>Three<br>

    <p>And here are my checkboxes</p>    
    <input type='checkbox' id="chkbox0" name="checkbox0" value="checkbox0">Checkbox Zero<br>
    <input type='checkbox' id="chkbox1" name="checkbox1" value="checkbox1">Checkbox One<br>
    <input type='checkbox' id="chkbox2" name="checkbox2" value="checkbox2">Checkbox Two<br>
    <input type='checkbox' id="chkbox3" name="checkbox3" value="checkbox3">Checkbox Three<br>
  </body>
</html>
1
  • 2
    @RejithRKrishnan prop() requires a property name and value if using as setter Commented May 28, 2016 at 17:14

2 Answers 2

2

I think this should do the trick (if I get your question correctly)

  $("input[type=radio]").click(function(e) {
    var whats_selected = $(this).val();
    // check an disable all checboxes
    $("input[type=checkbox]")
      .attr('checked', true)
      .prop('disabled', true);
    // enable the targetted checkbox
    $('#chkbox' + whats_selected)
      .prop('disabled', false);
  });

Have a look at the demo: https://jsfiddle.net/a5og0soL/

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

4 Comments

I don't think you need $("input[name=radio_group]").prop("checked", false); -- the radio buttons should do that by default. Otherwise, +1.
This works for Chrome but not on firefox. Firefox version is 46.0.1
@gibberish You mean in the fiddle right? True, I just copied a bit to much from the OP code...
@sri_84 works fine for me in Firefox 46.0.1 on Mac. Are you testing the fiddle or your own (edited) code? Perhaps something else is interfering...
1

Here is another way to do it, matching the index of the radio/checkbox pair:

jsFiddle Demo

var ndx;
$(document).ready(function() {

  $("input[type=radio]").click(function() {
	ndx = $("input[type=radio]").index(this);
    $("input[type=checkbox]").attr('checked', true).prop('disabled', true);
    $("input[type=checkbox]").eq(ndx).prop('disabled',false);
  });

}); //END document.ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>My 4 Radio Buttons</h3>

<input type="radio" name='radio_group' value=0>Zero
<br>
<input type="radio" name='radio_group' value=1>One
<br>
<input type="radio" name='radio_group' value=2>Two
<br>
<input type="radio" name='radio_group' value=3>Three
<br>

<p>And here are my checkboxes</p>
<input type='checkbox' id="chkbox0" name="checkbox0" value="checkbox0">Checkbox Zero
<br>
<input type='checkbox' id="chkbox1" name="checkbox1" value="checkbox1">Checkbox One
<br>
<input type='checkbox' id="chkbox2" name="checkbox2" value="checkbox2">Checkbox Two
<br>
<input type='checkbox' id="chkbox3" name="checkbox3" value="checkbox3">Checkbox Three
<br>

2 Comments

Why dit you make ndx a global? Also, you are assuming the radios and checkboxes will always be in the same order. That may work today but doesn't seem very future proof imo.
You are right - ndx doesn't need to be a global. When I started writing my answer, I was intending to remember the last-checked radio, but then didn't need to. Also, I was still remembering a previous question that was quite similar and was probably getting the two a bit confuzzled. As for matching the order, I only demonstrated that method because that was how OP did it, so just in case that was required.

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.