check several, but not all checkboxes with 1 click

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

    check several, but not all checkboxes with 1 click

    Hi,
    I have created a 12 month calendar where each day has a check box
    whereby the user can indicate if that day is available or not
    available for a certain event. The calendar is 'drawn' in a single
    form rather than 12 separate forms.

    If the checkbox contained in each day within each month has a unique
    name such as 1August2003, 2August2003, etc, is there a way in
    Javascript where I could have a button by each month where the user
    could click once to check each box within that month but not the other
    11 months? For example, I would like to have a link next to the
    January header where the user can select all days in January.

    I have seen posts where you can sleect all boxes within the form but
    that isn't exactly what I need...I need to somehow do that but only if
    the name contains a certain string (the month name in this case). Any
    help would be much appreciated.

    -John
  • Richard Hockey

    #2
    Re: check several, but not all checkboxes with 1 click

    perhaps this might be useful:

    <script type="text/javascript">
    var MonthDays=Array ("","31", "28", "31", "30", "31", "30", "31", "31", "30",
    "31", "30", "31");

    function MonthToggle(For mObject,CBMonth )
    {
    var daycount="01";
    for(n=1;n<=Mont hDays[parseInt(CBMont h,10)];n++)
    {
    if(n<10)
    daycount='0'+n;
    else
    daycount=n;
    var CBObject=FormOb ject.elements['m'+CBMonth+day count];

    // this toggles the state of each checkbox, depending on the current state
    // if you want to just set or unset all the checkboxes the code will be
    simpler
    // use CBObject.checke d=true/false; instead of the switch statements.

    switch(CBObject .checked)
    {
    case true:
    CBObject.checke d=false;
    break;
    case false:
    CBObject.checke d=true;
    break;
    }

    }
    }
    </script>

    assuming the form is layed out in the following manner:

    <form name="yearform" >
    <input type="checkbox" name="m0101" value="0101">1
    <input type="checkbox" name="m0102" value="0102">2
    <input type="checkbox" name="m0103" value="0103">3
    <input type="checkbox" name="m0104" value="0104">4
    .....
    <input type="checkbox" name="m0131" value="0131">31
    <a href="#" onClick="MonthT oggle(document. forms['yearform'],'01'); return
    false;">January </a>

    <input type="checkbox" name="m0201" value="0201">1
    <input type="checkbox" name="m0202" value="0202">2
    <input type="checkbox" name="m0203" value="0203">3
    <input type="checkbox" name="m0204" value="0204">4
    .....
    <input type="checkbox" name="m0228" value="0228">28
    <a href="#" onClick="MonthT oggle(document. forms['yearform'],'02'); return
    false;">JFebrua ry</a>

    ......

    <input type="checkbox" name="m1201" value="1201">1
    <input type="checkbox" name="m1202" value="1202">2
    <input type="checkbox" name="m1203" value="1203">3
    <input type="checkbox" name="m1204" value="1204">4
    .....
    <input type="checkbox" name="m1231" value="1231">31
    <a href="#" onClick="MonthT oggle(document. forms['yearform'],'12'); return
    false;">Decembe r</a>
    </form>

    tested in IE6, NetScape 7 and Opera 7.01

    "John Banta" <sigatropolis@y ahoo.com> wrote in message
    news:31ae79e9.0 307302225.126a3 1e4@posting.goo gle.com...[color=blue]
    > Hi,
    > I have created a 12 month calendar where each day has a check box
    > whereby the user can indicate if that day is available or not
    > available for a certain event. The calendar is 'drawn' in a single
    > form rather than 12 separate forms.
    >
    > If the checkbox contained in each day within each month has a unique
    > name such as 1August2003, 2August2003, etc, is there a way in
    > Javascript where I could have a button by each month where the user
    > could click once to check each box within that month but not the other
    > 11 months? For example, I would like to have a link next to the
    > January header where the user can select all days in January.
    >
    > I have seen posts where you can sleect all boxes within the form but
    > that isn't exactly what I need...I need to somehow do that but only if
    > the name contains a certain string (the month name in this case). Any
    > help would be much appreciated.
    >
    > -John[/color]


    Comment

    • Richard Hockey

      #3
      Re: check several, but not all checkboxes with 1 click


      "Richard Hockey" <richardhockey@ dsl.pipex.com> wrote in message
      news:3f28cd24$0 $963$cc9e4d1f@n ews.dial.pipex. com...[color=blue]
      > perhaps this might be useful:
      >
      > <script type="text/javascript">
      > var MonthDays=Array ("","31", "28", "31", "30", "31", "30", "31", "31",[/color]
      "30",[color=blue]
      > "31", "30", "31");
      >
      > function MonthToggle(For mObject,CBMonth )
      > {
      > var daycount="01";
      > for(n=1;n<=Mont hDays[parseInt(CBMont h,10)];n++)
      > {
      > if(n<10)
      > daycount='0'+n;
      > else
      > daycount=n;
      > var CBObject=FormOb ject.elements['m'+CBMonth+day count];
      >
      > // this toggles the state of each checkbox, depending on the current[/color]
      state[color=blue]
      > // if you want to just set or unset all the checkboxes the code will be
      > simpler
      > // use CBObject.checke d=true/false; instead of the switch statements.
      >
      > switch(CBObject .checked)
      > {
      > case true:
      > CBObject.checke d=false;
      > break;
      > case false:
      > CBObject.checke d=true;
      > break;
      > }[/color]

      doh! abandon the switch statement and replace it with:

      CBObject.checke d=!CBObject.che cked;

      to toggle the checkbox states
      [color=blue]
      >
      > }
      > }
      > </script>
      >
      > assuming the form is layed out in the following manner:
      >
      > <form name="yearform" >
      > <input type="checkbox" name="m0101" value="0101">1
      > <input type="checkbox" name="m0102" value="0102">2
      > <input type="checkbox" name="m0103" value="0103">3
      > <input type="checkbox" name="m0104" value="0104">4
      > ....
      > <input type="checkbox" name="m0131" value="0131">31
      > <a href="#" onClick="MonthT oggle(document. forms['yearform'],'01'); return
      > false;">January </a>
      >
      > <input type="checkbox" name="m0201" value="0201">1
      > <input type="checkbox" name="m0202" value="0202">2
      > <input type="checkbox" name="m0203" value="0203">3
      > <input type="checkbox" name="m0204" value="0204">4
      > ....
      > <input type="checkbox" name="m0228" value="0228">28
      > <a href="#" onClick="MonthT oggle(document. forms['yearform'],'02'); return
      > false;">JFebrua ry</a>
      >
      > .....
      >
      > <input type="checkbox" name="m1201" value="1201">1
      > <input type="checkbox" name="m1202" value="1202">2
      > <input type="checkbox" name="m1203" value="1203">3
      > <input type="checkbox" name="m1204" value="1204">4
      > ....
      > <input type="checkbox" name="m1231" value="1231">31
      > <a href="#" onClick="MonthT oggle(document. forms['yearform'],'12'); return
      > false;">Decembe r</a>
      > </form>
      >
      > tested in IE6, NetScape 7 and Opera 7.01
      >
      > "John Banta" <sigatropolis@y ahoo.com> wrote in message
      > news:31ae79e9.0 307302225.126a3 1e4@posting.goo gle.com...[color=green]
      > > Hi,
      > > I have created a 12 month calendar where each day has a check box
      > > whereby the user can indicate if that day is available or not
      > > available for a certain event. The calendar is 'drawn' in a single
      > > form rather than 12 separate forms.
      > >
      > > If the checkbox contained in each day within each month has a unique
      > > name such as 1August2003, 2August2003, etc, is there a way in
      > > Javascript where I could have a button by each month where the user
      > > could click once to check each box within that month but not the other
      > > 11 months? For example, I would like to have a link next to the
      > > January header where the user can select all days in January.
      > >
      > > I have seen posts where you can sleect all boxes within the form but
      > > that isn't exactly what I need...I need to somehow do that but only if
      > > the name contains a certain string (the month name in this case). Any
      > > help would be much appreciated.
      > >
      > > -John[/color]
      >
      >[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: check several, but not all checkboxes with 1 click

        sigatropolis@ya hoo.com (John Banta) writes:
        [color=blue]
        > I have created a 12 month calendar where each day has a check box
        > whereby the user can indicate if that day is available or not
        > available for a certain event. The calendar is 'drawn' in a single
        > form rather than 12 separate forms.[/color]

        Good. If you plan to submit the form, you only want one. And if you
        don't want to submit it, you don't need the form at all.
        [color=blue]
        > If the checkbox contained in each day within each month has a unique
        > name such as 1August2003, 2August2003,[/color]

        First: An HTML name/id is not allowed to start with a digit. You should
        change it to something like Date1August2003 .
        [color=blue]
        > etc, is there a way in Javascript where I could have a button by
        > each month where the user could click once to check each box within
        > that month but not the other 11 months? For example, I would like to
        > have a link next to the January header where the user can select all
        > days in January.[/color]

        Don't use a link if you are not referring to another page. Use a button.
        You know the month and year when you create the form, so you can have
        the button be something like

        <input type="button" value="Select All"
        onclick="select AllDays(this.fo rm,'January',20 03)">

        and the function is:

        function selectAllDays(f orm,mth,yr) {
        var name;
        for(var i=1;form.elemen ts[name="Date"+i+m th+yr];i++) {
        form.elements[name].checked=true;
        }
        }


        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • John Banta

          #5
          Re: check several, but not all checkboxes with 1 click

          Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<65ljyn3p. fsf@hotpop.com> ...
          [color=blue]
          > First: An HTML name/id is not allowed to start with a digit. You should
          > change it to something like Date1August2003 .[/color]

          Thanks for the info. I am not questioning you but I am fairly new to
          this and would like to understand what you mean by "HTML name/id is
          not allowed to start with a digit"? My form appears to work as I am
          expecting it to so is there a browser problem that I may run into or
          is it bad form, etc?
          [color=blue]
          > Don't use a link if you are not referring to another page. Use a button.
          > You know the month and year when you create the form, so you can have
          > the button be something like[/color]

          Yes, I agree. This was my poor choice of wording...I am intedning to
          use a button.

          -John

          Comment

          • Dr John Stockton

            #6
            Re: check several, but not all checkboxes with 1 click

            JRS: In article <3f28cd24$0$963 $cc9e4d1f@news. dial.pipex.com> , seen in
            news:comp.lang. javascript, Richard Hockey <richardhockey@ dsl.pipex.com>
            posted at Thu, 31 Jul 2003 09:02:37 :-[color=blue]
            >perhaps this might be useful:
            >
            ><script type="text/javascript">
            >var MonthDays=Array ("","31", "28", "31", "30", "31", "30", "31", "31", "30",
            >"31", "30", "31");[/color]

            Except in a Leap Year.

            function LastOfMonth(D) { // Date Object, string, or number
            with (new Date(D)) { setDate(32) ; return 32-getDate() } }


            Responses should follow trimmed quotes - see FAQ,

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: check several, but not all checkboxes with 1 click

              sigatropolis@ya hoo.com (John Banta) writes:
              [color=blue]
              > Thanks for the info. I am not questioning you but I am fairly new to
              > this and would like to understand what you mean by "HTML name/id is
              > not allowed to start with a digit"?[/color]

              According to the HTML 4.0 specification (and all later HTML and XHTML
              specifications) , the values of "id" and "name" attributes are not
              allowed to begin with a digit.
              <URL:http://www.w3.org/TR/1998/REC-html40-19980424/types.html#type-name>
              [color=blue]
              > My form appears to work as I am expecting it to so is there a
              > browser problem that I may run into or is it bad form, etc?[/color]

              Browsers are very forgiving creatures. They have to be, since many web
              authors don't know, or don't care, that there are rules for HTML, and
              happily accepts anything that works in their favorite browser.

              You will *probably* not run into any problems. The keyword is
              "probably".

              When using Javascript to access form elements with the dot-notation,
              e.g.
              <form id="foo"><inpu t type="text" id="bar"></form>
              accessed using
              document.forms. foo.elements.ba r.value
              it is important that the id-values does not start with a number.
              The following is syntactically incorrect:
              document.forms. 2foo

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • YD

                #8
                Re: check several, but not all checkboxes with 1 click

                Lasse Reichstein Nielsen wrote:[color=blue]
                > When using Javascript to access form elements with the dot-notation,
                > e.g.
                > <form id="foo"><inpu t type="text" id="bar"></form>
                > accessed using
                > document.forms. foo.elements.ba r.value
                > it is important that the id-values does not start with a number.[/color]

                Are you sure you can use id instead of name for the elements of a
                form? I mean the element won't be submitted if no name attribute was
                given. I know the use of id or name for the DOM is equivalent to
                retrieve an element and the use of the name attribute for form is
                deprecated.

                --
                Y.D.


                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: check several, but not all checkboxes with 1 click

                  "YD" <yd-news@free.fr> writes:
                  [color=blue]
                  > Are you sure you can use id instead of name for the elements of a
                  > form? I mean the element won't be submitted if no name attribute was
                  > given.[/color]

                  You can use it, and it won't be submitted.
                  I am mostly used to writing forms that are not submitted. The point
                  in this case is the same for "name".
                  [color=blue]
                  > I know the use of id or name for the DOM is equivalent to
                  > retrieve an element and the use of the name attribute for form is
                  > deprecated.[/color]

                  It has, for form, but not for elements. Personally, I would use both
                  id and name on the elements, and with different values. The name is
                  for submitting, an need only be unique within the form, and the id is
                  for accessing on the client and should be unique within the page.

                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  Working...