mercredi 22 juillet 2015

Showing how many form checkboxes are checked in document itself

I have coded an ASP page using an example here that shows how many checked boxes there are in a form in a pop-up alert box. The problem is that we now have so many boxes on the form that it takes forever to select a box, close the alert, check another box, close the alert… I would like to change or replace the code to show the number of checked boxes in the document's HTML instead of an alert, updating the count as boxes are checked or unchecked. Here is the script used to count the boxes and show the alert:

<script language="JavaScript">
function checkTotalCheckedBoxes()
{
     var checkLength = 0;
     var boxes = document.getElementById("yyy").getElementsByTagName("input");
     for (var i = 0; i < boxes.length; i++)
     {
         boxes[i].checked ? checkLength++ : null;
     }
     alert (checkLength + " boxes are checked." );
}
</script>

I also have a "Select All" script that works great for selecting all the boxes at once. The only problem is the "Select All" box stays checked once used, even if some of the boxes are de-selected. I need to modify the script to uncheck the "Select All" box is the user clicks it, then de-selects any of the checked boxes.

Here is that script:

<script language="JavaScript">
function toggle(source) {
  checkboxes = document.getElementsByName('checkbox');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  } 

       var checkLength = 0;
     var boxes = document.getElementById("yyy").getElementsByTagName("input");
     for (var i = 0; i < boxes.length; i++)
     {
         boxes[i].checked ? checkLength++ : null;
     }
     alert (checkLength + " boxes are checked." );
}
</script>

Many thanks for any help you can offer!




Aucun commentaire:

Enregistrer un commentaire