Sushil Bhardwaj
Sushil Bhardwaj

Reputation: 151

how to deselect a particular selected checkbox when condition fails

I want to deselect a particular selected checkbox when condition fails.i have selected number of checkboxes but when my condition fails, i want to show there is alert message and don't want to select that checkbox. But when i am doing this, there is alert message but checkbox is selected.I want to unselect this selected checkbox when message is displaying

    <input type="checkbox" id="ChkIds_<?php echo $row['leader_id']; ?>" value="" name="selected[]" onChange="myFunction(this)"  class="<?php echo $row['state_point'];?>">

          <td align="center" bgcolor="#FFFFFF"><span id="total" class="0">1000</span></td>


<script type = "text/javascript">


function myFunction(obj) {
                   if(obj.checked==1) {
                          var valuenew=obj.value;
                          var NewVal=$('#total').text();


                          var calcVal=Number(NewVal)-Number(valuenew);
                            if(calcVal<=0)
                          {
                         alert('hi');
                          }
                          else
                          {
                          $('#total').text(calcVal);
                          obj.id.checked=false;
                         }
                          }
                  else {
                         var valuenew=obj.value;
                         var NewVal=$('#total').text();
                         var calcVal=Number(NewVal)+Number(valuenew);
                         $('#total').text(calcVal);  }



                       } 

</script>

Upvotes: 1

Views: 255

Answers (2)

Balint Bako
Balint Bako

Reputation: 2560

If you want to uncheck the checkbox which was just check, then:

$(obj).attr("checked", false);

If another one:

$("#checkboxid").attr("checked", false);

If all of them:

$("input:checkbox").attr("checked", false);

Upvotes: 1

user2511671
user2511671

Reputation:

You can call a function onchange and uncheck the desired checkbox

$("#chkbox_id:checked").attr("checked", false);

Upvotes: 1

Related Questions