473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 20 '05 #1
8 3996
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(FormObject,CBMonth)
{
var daycount="01";
for(n=1;n<=MonthDays[parseInt(CBMonth,10)];n++)
{
if(n<10)
daycount='0'+n;
else
daycount=n;
var CBObject=FormObject.elements['m'+CBMonth+daycount];

// 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.checked=true/false; instead of the switch statements.

switch(CBObject.checked)
{
case true:
CBObject.checked=false;
break;
case false:
CBObject.checked=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="MonthToggle(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="MonthToggle(document.forms['yearform'],'02'); return
false;">JFebruary</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="MonthToggle(document.forms['yearform'],'12'); return
false;">December</a>
</form>

tested in IE6, NetScape 7 and Opera 7.01

"John Banta" <si**********@yahoo.com> wrote in message
news:31**************************@posting.google.c om...
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

Jul 20 '05 #2

"Richard Hockey" <ri***********@dsl.pipex.com> wrote in message
news:3f*********************@news.dial.pipex.com.. .
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(FormObject,CBMonth)
{
var daycount="01";
for(n=1;n<=MonthDays[parseInt(CBMonth,10)];n++)
{
if(n<10)
daycount='0'+n;
else
daycount=n;
var CBObject=FormObject.elements['m'+CBMonth+daycount];

// 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.checked=true/false; instead of the switch statements.

switch(CBObject.checked)
{
case true:
CBObject.checked=false;
break;
case false:
CBObject.checked=true;
break;
}
doh! abandon the switch statement and replace it with:

CBObject.checked=!CBObject.checked;

to toggle the checkbox states

}
}
</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="MonthToggle(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="MonthToggle(document.forms['yearform'],'02'); return
false;">JFebruary</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="MonthToggle(document.forms['yearform'],'12'); return
false;">December</a>
</form>

tested in IE6, NetScape 7 and Opera 7.01

"John Banta" <si**********@yahoo.com> wrote in message
news:31**************************@posting.google.c om...
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


Jul 20 '05 #3
si**********@yahoo.com (John Banta) writes:
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.
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.
If the checkbox contained in each day within each month has a unique
name such as 1August2003, 2August2003,
First: An HTML name/id is not allowed to start with a digit. You should
change it to something like Date1August2003.
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.


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="selectAllDays(this.form,'January',2003)">

and the function is:

function selectAllDays(form,mth,yr) {
var name;
for(var i=1;form.elements[name="Date"+i+mth+yr];i++) {
form.elements[name].checked=true;
}
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<65**********@hotpop.com>...
First: An HTML name/id is not allowed to start with a digit. You should
change it to something like Date1August2003.
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?
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


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

-John
Jul 20 '05 #5
JRS: In article <3f*********************@news.dial.pipex.com>, seen in
news:comp.lang.javascript, Richard Hockey <ri***********@dsl.pipex.com>
posted at Thu, 31 Jul 2003 09:02:37 :-
perhaps this might be useful:

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


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.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6
si**********@yahoo.com (John Banta) writes:
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"?
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>
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?


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"><input type="text" id="bar"></form>
accessed using
document.forms.foo.elements.bar.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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
YD
Lasse Reichstein Nielsen wrote:
When using Javascript to access form elements with the dot-notation,
e.g.
<form id="foo"><input type="text" id="bar"></form>
accessed using
document.forms.foo.elements.bar.value
it is important that the id-values does not start with a number.


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.
Jul 20 '05 #8
"YD" <yd*****@free.fr> writes:
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.
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".
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.


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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Adrian Parker | last post by:
I have a PHP generated page which displays X many records. Each record has a checkbox preceding it. The user checks several checkboxes, and hits a delete button. All the corresponding records...
5
by: NotGiven | last post by:
I have a form with several questions. Within each question there are several checkboxes. I need to ensure that the user checks at least one checkbox. They can check more but must check at least...
2
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
4
by: Shawn | last post by:
Hi. I have a ToolBar with a couple of ToolBarButtons. On postback after clicking on of the buttons Page_Load is called first then the ToolBarButton's click event is called. Is there anyway for...
6
by: Mike | last post by:
I am just beginning with VB and had been using a crippled version of VB 6.0 which works fine except there's limited information resouces with it. So, like a good scout, I got a learning edition of...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
1
Frinavale
by: Frinavale | last post by:
I'm working on an ASP.NET application using VB.NET for server side code. I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this...
3
by: BibhuAshish | last post by:
Hi, I want to check or uncheck a number of checkboxes by clicking an image. My html code is given below: <TABLE> <TR> <TD>
1
by: Anuj | last post by:
Hi, since sometime I'm stuck in a problem where I want to check or uncheck all the checkboxes. If I'm choosing name for the checkbox array as 'chkbx_ary' then I'm able to check/uncheck all the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.