Having Max 3 Checkboxes Checked Using Javascript Or Jquery
So I have this pizza-ordering application for my final project, but I need to have the max amounts of toppings be two, so I have to limit my checkable checkboxes to two, I was thin
Solution 1:
Your counter variable is getting reset to zero each time they click a checkbox. You should move it out of the function:
<scripttype="text/javascript">varNewCount = 0;
   functionKeepCount() 
   {
       if (document.theform.meat[].checked)
       {NewCount = NewCount + 1}
       if (document.theform.cheese[].checked)
       {NewCount = NewCount + 1}
       if (document.theform.greens[].checked)
       {NewCount = NewCount + 1}
       if (NewCount == 3)
       {
           alert('Pick Just Two Please')
           document.first,second,thinrd; returnfalse;
       }
   } 
</script>Solution 2:
functionKeepCount() {
  // Count the number of checkmarks in each category.functioncountChecked(list) {
    var count = 0;
    for (var i = 0; i < list.length; i++) {
      var el = list[i];
      if (el.checked) {
        count++;
      }
    }
    return count;
  }
  varNewCount = 0// Check the meatsvar topping = document.getElementsByName("meat[]");
  NewCount += countChecked(topping);
  var topping = document.getElementsByName("cheese[]");
  NewCount += countChecked(topping);
  var topping = document.getElementsByName("greens[]");
  NewCount += countChecked(topping);
  if (NewCount >= 3) {
    alert('Pick Just Two Please')
    returnfalse;
  }
}
Solution 3:
You can get rid of all the onClick inline calls in your HTML if you use the following, this also will prevent subsequent checkbox options from being selected:
$(':input').click(function(event) {
    var count = 0;
    $(':input').each(function () {
        if (this.checked)
        {
            count++;   
        }
    });
    if (count > 2)
    {
        event.preventDefault();
        alert('Pick just two please!');
    }
});
jsFiddle: http://jsfiddle.net/72CGc/35/
Solution 4:
With jquery
<script>
    $().ready(function () {
        // your topping 'names' herevar groups = ['meat[]', 'cheese[]', 'greens[]'];
        // for each group..
        $.each(groups, function (idx, groupName) {
            // ..find all checkboxes insidevar $group = $('.col-lg-2').find('input[name="' + groupName + '"]');
            // and catch click
            $group.click({ group: $group }, function (event) {
                // on click event calculate sum of checked checkboxesvar count = 0;
                $.each(event.data.group, function (idx, input) {
                    count += (input.checked ? 1 : 0);
                }); // each input in group// warning the userif (count > 2) {
                    alert('2 only!');
                    event.preventDefault();
                    return;
                }
            });
        }); // each topping
    });
</script>jsFiddle: http://jsfiddle.net/qAsR3/
Post a Comment for "Having Max 3 Checkboxes Checked Using Javascript Or Jquery"