473,395 Members | 1,774 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,395 software developers and data experts.

Multiple Checkboxes

I have a total of 8 checkboxes, but the user is only allowed to check
any three of them. After have been checked I have an alert box saying
that only 3 boxes can be checked. I'm trying to get so that it
disable's the checkboxes that are not checked, but then if one of the
three get unchecked, then all checkboxes become enabled again. Here's
my code so far....(In Javascript)

function chkCount()
{
var i = 0;

if (document.frmShortService.chkLH.checked) i++;
if (document.frmShortService.chkLTL.checked) i++;
....

if (i > 3)
{
alert("Only 3 Checkboxes can be checked");
return(false);
}
}

<input type="checkbox" name="chkLH" value="YES" onclick="chkCount()">
.....

Thanks in Advance!

Dave
Jul 23 '05 #1
5 2181
Im not gonna do the code for you, but i'll explain the approach you should
use...

create a global variable eg. cCount
initially set it to 0 --> var cCount = 0

change you chkCount function to chkCount(targ) where targ will be sent as
onclick="chkCount(this)"
so you know which checkbox was clicked..

everytime you call your chkCount function do this..
check whether the chekbox is now checked or unchecked:
if (targ.checked) {

} else {

}...
check the value of cCount
if the box is being unchecked, simply do this: cCount-- and nothing else
(get that from ur condition above)

if it is being checked...
if less than 3, do nothing, just increment cCount --> cCount++
if not, then you don't want that one checked, so:
targ.checked = false
where targ is the checkbox that was just clicked...

you can alert the user and ask them to uncheck something first blah blah

i hope you get the picture...
come to think of it, i coulda just done the function for you..., but then
you won't learn will you :0)

good luck!
"Dave D." <da*****@charter.net> wrote in message
news:2f**************************@posting.google.c om...
I have a total of 8 checkboxes, but the user is only allowed to check
any three of them. After have been checked I have an alert box saying
that only 3 boxes can be checked. I'm trying to get so that it
disable's the checkboxes that are not checked, but then if one of the
three get unchecked, then all checkboxes become enabled again. Here's
my code so far....(In Javascript)

function chkCount()
{
var i = 0;

if (document.frmShortService.chkLH.checked) i++;
if (document.frmShortService.chkLTL.checked) i++;
...

if (i > 3)
{
alert("Only 3 Checkboxes can be checked");
return(false);
}
}

<input type="checkbox" name="chkLH" value="YES" onclick="chkCount()">
....

Thanks in Advance!

Dave

Jul 23 '05 #2
Dave D. wrote:
I have a total of 8 checkboxes, but the user is only allowed to check
any three of them. After have been checked I have an alert box saying
that only 3 boxes can be checked. I'm trying to get so that it
disable's the checkboxes that are not checked, but then if one of the
three get unchecked, then all checkboxes become enabled again.


Rather than disabling the checkboxes after 3 are picked, why not just not
allow them to check them? onClick, check if 3 are checked. If so, then
uncheck the one they just checked and show them an alert.

I have a library which does all this for you, with very minimal coding
required. If you want to check it out, look at
http://www.mattkruse.com/javascript/checkboxgroup/

Good luck!

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 23 '05 #3
what if they checked more than that?
you still have to keep track, else you gotta go uncheck everything they did,
not very pratical if you're working with a checklist of more than just 8,
and the user won't really like having to redo everything if everything is
uncheck coz he didn't understand i was sposed to check a limited number of
items... stop the user WHEN he/she makes the mistake.

cheers
:o)

"Matt Kruse" <ne********@mattkruse.com> wrote in message
news:c7*********@news4.newsguy.com...
Dave D. wrote:
I have a total of 8 checkboxes, but the user is only allowed to check
any three of them. After have been checked I have an alert box saying
that only 3 boxes can be checked. I'm trying to get so that it
disable's the checkboxes that are not checked, but then if one of the
three get unchecked, then all checkboxes become enabled again.


Rather than disabling the checkboxes after 3 are picked, why not just not
allow them to check them? onClick, check if 3 are checked. If so, then
uncheck the one they just checked and show them an alert.

I have a library which does all this for you, with very minimal coding
required. If you want to check it out, look at
http://www.mattkruse.com/javascript/checkboxgroup/

Good luck!

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/

Jul 23 '05 #4
So basically just increment i in a for loop? Is there a command to
"uncheck"? Or undo the last action the user did?

Thanks
Dave

"Dominique" <ni****@webadstudio.com> wrote in message news:<c7**********@ctb-nnrp2.saix.net>...
what if they checked more than that?
you still have to keep track, else you gotta go uncheck everything they did,
not very pratical if you're working with a checklist of more than just 8,
and the user won't really like having to redo everything if everything is
uncheck coz he didn't understand i was sposed to check a limited number of
items... stop the user WHEN he/she makes the mistake.

cheers
:o)

"Matt Kruse" <ne********@mattkruse.com> wrote in message
news:c7*********@news4.newsguy.com...
Dave D. wrote:
I have a total of 8 checkboxes, but the user is only allowed to check
any three of them. After have been checked I have an alert box saying
that only 3 boxes can be checked. I'm trying to get so that it
disable's the checkboxes that are not checked, but then if one of the
three get unchecked, then all checkboxes become enabled again.


Rather than disabling the checkboxes after 3 are picked, why not just not
allow them to check them? onClick, check if 3 are checked. If so, then
uncheck the one they just checked and show them an alert.

I have a library which does all this for you, with very minimal coding
required. If you want to check it out, look at
http://www.mattkruse.com/javascript/checkboxgroup/

Good luck!

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/

Jul 23 '05 #5
Dominique wrote:
what if they checked more than that?


You misunderstood.

What I was saying was, don't disable the remaining checkboxes. Instead, as
soon as they check one more than they are allowed to, throw up an alert
message, and then uncheck the box. So it's impossible for them to check more
than what is allowed. This is much easier than disabling all the remaining
checkboxes, IMO.

--
Matt Kruse
Javascript Toolbox: http://www.mattkruse.com/javascript/
Jul 23 '05 #6

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
6
by: Emmett Power | last post by:
Hi, I have a form on a web page with a number of radio buttons bound to the same field. Is it possible to set up the form so that users can select more than one radio button to submit multiple...
8
by: Ralph Freshour | last post by:
I have multiple checkbox's created with an array name because I have many on the same web page - their names are like: frm_chk_delete frm_chk_delete frm_chk_delete frm_chk_delete etc. Here...
5
by: @(none) | last post by:
I have a page which is a set of CheckBoxes generated daily and thus the number of Checkboxes changes each day. What I want to do is allow the user to select one or more checkboxes and the push a...
8
by: Tim | last post by:
On my form I have 10 checkboxes named chkbox1,chkbox2,....chkbox10. I would have like to set it up as an array control like in VB6 where I could have chkbox(1),chkbox(2) but I think .net you have...
6
by: jeffsnox | last post by:
Hi, I have multiple checkboxes on the same form as follows: <input type='checkbox' name='cbtype' value='1'> <input type='checkbox' name='cbtype' value='2'> <input type='checkbox'...
1
by: projectjakecs | last post by:
Hi, I am working on a ms access database and I am having trouble using a form to create new records in an associative table. Here is the breakdown of my database: Main Table - Computer...
4
by: ramapv | last post by:
can i highlight a checkbox from a group of checkbox with particular name which is given as a search key. I am having a list of checkboxes and i have to select some of them and form a group.but i'm...
5
by: TechnoAtif | last post by:
Hi All..'mAtif..i've got stuck within checkboxes these days..i've got many input items like checkboxes,textarea and along with them there are many checkboxes...all the data except the checkbox's is...
0
by: Ned Balzer | last post by:
Hi, Can anyone point me in the direction of a solution for validating multiple checkboxes in an asp.net 2.0 page? 1) This is not a checkboxlist, it is a number of separate checkboxes 2) I do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.