5

I have group of checkbox buttons, if I click and check one I want other check boxes to be set to disable. Also if I click to the same and uncheck I want all of them to be enabled. My logic works if I checked the checkbox but if I click again all of them are disabled. Here is my code:

<tr><td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td></tr>

function ckChange(ckType){
        var ckName = document.getElementsByName(ckType.name);

        for(var i=0; i < ckName.length; i++){
            if(!ckName[i].checked){
                ckName[i].disabled = true;
            }else{
                ckName[i].disabled = false;
            }
        } 
    }

My current code is failing to set disable attribute to true and false when check box is checked. If you see where is the problem in my code please let me know. I have tried to make this function to work for any set of checkboxes. If you see any way to improve this I would like to hear that.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

5 Answers5

8

add a simple if else statement that checks if the checkbox is checked or not. if its checked, disable all other check boxes. if not checked, enable all check boxes

<tr><td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td></tr>
<tr><td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td></tr>

<script>
function ckChange(ckType){
    var ckName = document.getElementsByName(ckType.name);
    var checked = document.getElementById(ckType.id);

    if (checked.checked) {
      for(var i=0; i < ckName.length; i++){

          if(!ckName[i].checked){
              ckName[i].disabled = true;
          }else{
              ckName[i].disabled = false;
          }
      } 
    }
    else {
      for(var i=0; i < ckName.length; i++){
        ckName[i].disabled = false;
      } 
    }    
}
</script>

ps. the problem with your code is that the for loop is executing no matter what. so if you check something, it disables all unchecked, but if you uncheck something, the loops disables all checkboxes since all of them are now unchecked. thats why the if statement I added works. it differentiates if the checkbox is being checked or unchecked and then executes the desired functionality.

indubitablee
  • 8,136
  • 2
  • 25
  • 49
  • I have a question and I'm not sure if this is the best way to do it. Once I load my page I have to check my check box based on the value from Database. One of the check boxes from the group might be checked, if it is check then I have to disable the rest of the check boxes with the same element name. Should I try to use the same function or that should be separate javascript function? – espresso_coffee Apr 14 '17 at 13:37
  • @espresso_coffee you can use the same function. you can trigger the click once the page loads based on the data from the data base – indubitablee Apr 14 '17 at 14:22
  • But I will pass the value in my function, and that's different than how function works now. I tried and that did not work. Here is what I have tried: ckChange('#Trim(myData.progress)#'); – espresso_coffee Apr 14 '17 at 14:33
  • you can trigger the click of that element with jquery as if you were clicking it on the dom – indubitablee Apr 14 '17 at 14:34
  • http://stackoverflow.com/questions/8206565/check-uncheck-checkbox-with-javascript – indubitablee Apr 14 '17 at 14:36
6

Same thing again, shorter code. Logically "the current checkbox should be enabled iff (the clicked checkbox is not checked) or (the current checkbox is the clicked checkbox)"

function ckChange(el) {
  var ckName = document.getElementsByName(el.name);
  for (var i = 0, c; c = ckName[i]; i++) {
   c.disabled = !(!el.checked || c === el);
  }
}
<tr>
  <td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td>
</tr>
James
  • 20,957
  • 5
  • 26
  • 41
3

There you go, working as you want.

function ckChange(ckType) {
  var ckName = document.getElementsByName(ckType.name);

  for (var i = 0; i < ckName.length; i++) {
    if (!ckType.checked) {
      ckName[i].disabled = false;
    } else {
      if (!ckName[i].checked) {
        ckName[i].disabled = true;
      } else {
        ckName[i].disabled = false;
      }
    }
  }

}
<tr>
  <td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td>
</tr>
Oen44
  • 3,156
  • 1
  • 22
  • 31
1

Another possible implementation:

function ckChange(el) {
  var ckName = document.getElementsByName(el.name);
  if (el.checked) {
    for (var i = 0; i < ckName.length; i++) {
      ckName[i].disabled = ckName[i] !== el;
    }
  } else {
    for (var i = 0; i < ckName.length; i++) {
      ckName[i].disabled = false
    }
  }

}
<tr>
  <td><input type="checkbox" name="progress" id="progress1" value="1" tabIndex="1" onClick="ckChange(this)">Test 1</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress2" value="1" tabIndex="1" onClick="ckChange(this)">Test 2</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress3" value="1" tabIndex="1" onClick="ckChange(this)">Test 3</td>
</tr>
<tr>
  <td><input type="checkbox" name="progress" id="progress4" value="1" tabIndex="1" onClick="ckChange(this)">Test 4</td>
</tr>
Rafael Kennedy
  • 964
  • 5
  • 16
0

My implementation, i little dirty. I'll refactor later

function DsChecks(value){   var chcks = []
      chcks = document.getElementsByName("tipoPerfil")
      for(var i = 0; i < chcks.length; i++){
           chcks[i].disabled = true;
      }
      for(var i = 0; i < chcks.length; i++){
         if(chcks[i].value == value && chcks[i].checked){
              chcks[i].disabled = false;
         }
         else if(chcks[i].value == value && !chcks[i].checked){
          for(var i = 0; i < chcks.length; i++){
          chcks[i].disabled = false;
            }
         }
      }

 } 
Vishal
  • 639
  • 7
  • 32
Maya
  • 75
  • 1
  • 3