Multiple Checkboxes

Discussion in 'Javascript' started by Dave D., May 7, 2004.

  1. Dave D.

    Dave D. Guest

    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
     
    Dave D., May 7, 2004
    #1
    1. Advertising

  2. Dave D.

    Dominique Guest

    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." <> wrote in message
    news:...
    > 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
     
    Dominique, May 7, 2004
    #2
    1. Advertising

  3. Dave D.

    Matt Kruse Guest

    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/
     
    Matt Kruse, May 7, 2004
    #3
  4. Dave D.

    Dominique Guest

    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
    :eek:)

    "Matt Kruse" <> wrote in message
    news:...
    > 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/
    >
    >
     
    Dominique, May 10, 2004
    #4
  5. Dave D.

    Dave D. Guest

    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" <> wrote in message news:<c7nbmp$kb2$>...
    > 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
    > :eek:)
    >
    > "Matt Kruse" <> wrote in message
    > news:...
    > > 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/
    > >
    > >
     
    Dave D., May 10, 2004
    #5
  6. Dave D.

    Matt Kruse Guest

    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/
     
    Matt Kruse, May 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YSBkZXZlbG9wZXI=?=

    datagrid and multiple checkboxes(hotmail model)

    =?Utf-8?B?YSBkZXZlbG9wZXI=?=, Nov 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    539
    Olivier Verdin
    Nov 6, 2004
  2. Peter Afonin

    Select multiple checkboxes

    Peter Afonin, May 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,896
    Peter Afonin
    May 11, 2005
  3. Patrick.O.Ige
    Replies:
    0
    Views:
    681
    Patrick.O.Ige
    Nov 3, 2005
  4. Replies:
    0
    Views:
    5,591
  5. GuyBrush Treepwood

    Struts - multiple actions for same checkboxes

    GuyBrush Treepwood, May 12, 2005, in forum: Java
    Replies:
    1
    Views:
    4,919
    Wendy Smoak
    May 12, 2005
Loading...

Share This Page