Checking selections made in a checkbox list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Menelty

    Checking selections made in a checkbox list

    I have 2 datagrids on my page and each one has a template column which
    has a checkbox relating to each specific row. I usually have one on
    the page and use javascript to make sure there has been x number of
    selections before allowing the user to proceed. I use the code below
    for this:

    for (var n=0; n < document.forms[0].length; n++)
    {
    if (document.forms[0].elements[n].type=='checkbo x')
    {
    if (document.forms[0].elements[n].checked)
    {
    i++;
    }
    }
    }

    How can I make sure there has been a selection made in each of the 2
    separate datagrids as my old way checks the entire page only? I am
    thinking I can specifically name the datagrid I want to check but cant
    remember how to do it.
    Am looking for a quick solution as short on time.

    Thanks.
  • Grant Wagner

    #2
    Re: Checking selections made in a checkbox list

    Menelty wrote:
    [color=blue]
    > I have 2 datagrids on my page and each one has a template column which
    > has a checkbox relating to each specific row. I usually have one on
    > the page and use javascript to make sure there has been x number of
    > selections before allowing the user to proceed. I use the code below
    > for this:
    >
    > for (var n=0; n < document.forms[0].length; n++)
    > {
    > if (document.forms[0].elements[n].type=='checkbo x')
    > {
    > if (document.forms[0].elements[n].checked)
    > {
    > i++;
    > }
    > }
    > }
    >
    > How can I make sure there has been a selection made in each of the 2
    > separate datagrids as my old way checks the entire page only? I am
    > thinking I can specifically name the datagrid I want to check but cant
    > remember how to do it.
    > Am looking for a quick solution as short on time.
    >
    > Thanks.[/color]

    Name the inputs in a way that uniquely identifies them:

    <input type="checkbox" name="grid1_One ">
    <input type="checkbox" name="grid1_Two ">
    <!-- etc -->
    and
    <input type="checkbox" name="grid2_One ">
    <input type="checkbox" name="grid2_Two ">
    <!-- etc -->

    And then count each "grid#_" set separately:

    function countGridsCheck ed() {
    var f = document.forms['theForm'].elements;
    var theGrids = new Object();
    var reGridIdentifie r = /^(grid\d+)_/;
    for (var n=0; n < f.length; n++) {
    if (f[n].type=='checkbo x' &&
    reGridIndentifi er.test(f[n].name) &&
    f[n].checked) {

    // it's a checkbox;
    // and it's name is "grid#_..."
    // and it's checked

    if (theGrids[RegExp.$1]) {
    // counted at least one in this "grid#_..."
    // increment the count
    theGrids[RegExp.$1]++;
    } else {
    // never counted anything in this "grid#_..."
    // count the first one
    theGrids[RegExp.$1] = 1;
    }
    }

    // return the object
    // theGrids['grid1'], theGrids['grid2'], etc
    return theGrids;
    }

    Now you can run:

    var theCounts = countGridsCheck ed();
    if (theCounts['grid1']) {
    // the count of "grid1" is non-null and non-zero
    } else {
    // the count of "grid1" is null or zero
    }

    This scales nicely because you can add addition "grids" by simply creating
    another set of checkboxes, the function itself would simply create another
    property on the returned object which you could test.

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 7 / Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Stephen Chalmers

      #3
      Re: Checking selections made in a checkbox list


      "Menelty" <m_nelson3@hotm ail.com> wrote in message
      news:a62babfb.0 406110209.420d9 68f@posting.goo gle.com...[color=blue]
      > I have 2 datagrids on my page and each one has a template column which
      > has a checkbox relating to each specific row. I usually have one on
      > the page and use javascript to make sure there has been x number of
      > selections before allowing the user to proceed. I use the code below
      > for this:
      >
      > for (var n=0; n < document.forms[0].length; n++)
      > {
      > if (document.forms[0].elements[n].type=='checkbo x')
      > {
      > if (document.forms[0].elements[n].checked)
      > {
      > i++;
      > }
      > }
      > }
      >
      > How can I make sure there has been a selection made in each of the 2
      > separate datagrids as my old way checks the entire page only? I am
      > thinking I can specifically name the datagrid I want to check but cant
      > remember how to do it.
      > Am looking for a quick solution as short on time.
      >
      > Thanks.[/color]

      Give all the checkboxes in each set the same unique name, then have your
      function scan for that name in all the elements of a named form.

      function cbCounter(formN ame, rangeName)
      {
      var fe=document.for ms[formName].elements, count=0;

      for( var i=0; i<fe.length; i++)
      if( fe[i].name==rangeNam e && fe[i].type=='checkbo x' && fe[i].checked )
      count++;

      return count;
      }

      So say you had a form called "form1" containing a set of checkboxes named
      "grid1", you could count the number checked with:

      n = cbCounter("form 1","grid1");

      --
      S.C.


      Comment

      Working...