Newbie: Selecting default checkbox?

Discussion in 'Javascript' started by J Belly, Sep 20, 2004.

  1. J Belly

    J Belly Guest

    Hi, all:

    I'm hoping mine is a simple problem:

    Basically, I have 15 checkboxes, and one of them (default) is
    pre-checked when the page is loaded. If the user then selects any of
    the 14 other checkboxes listed, how do I get the default one to
    automatically uncheck? (To put it another way, if the user selects
    all 14 checkboxes and then changes his mind and selects the default
    one again, I want the other 14 to be de-selected.)

    Thanks for the help!
    J
    J Belly, Sep 20, 2004
    #1
    1. Advertising

  2. J Belly

    McKirahan Guest

    "J Belly" <> wrote in message
    news:...
    > Hi, all:
    >
    > I'm hoping mine is a simple problem:
    >
    > Basically, I have 15 checkboxes, and one of them (default) is
    > pre-checked when the page is loaded. If the user then selects any of
    > the 14 other checkboxes listed, how do I get the default one to
    > automatically uncheck? (To put it another way, if the user selects
    > all 14 checkboxes and then changes his mind and selects the default
    > one again, I want the other 14 to be de-selected.)
    >
    > Thanks for the help!
    > J


    You could use radio buttons which automatically handle it but. like you, I
    prefer the appearance of a checkbox. Try the following; watch for
    word-wrap.

    <html>
    <head>
    <title>checkonly.htm</title>
    <script type="text/javascript">
    function checkonly(what) {
    var form = document.form1;
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements.type == "checkbox") {
    if (form.elements.name != what) {
    form.elements.checked = false;
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="form1" action="" method="post">
    <input type="checkbox" name="x0" onclick="checkonly('x0')">
    <input type="checkbox" name="x1" onclick="checkonly('x1')">
    <input type="checkbox" name="x2" onclick="checkonly('x2')">
    </form>
    </body>
    </html>
    McKirahan, Sep 20, 2004
    #2
    1. Advertising

  3. J Belly

    J Belly Guest

    McKirahan:

    Thanks for the help, but it didn't work. (By the way, the reason I
    can't use radio buttons is because I want the user to be able to check
    more than one box, while keeping just one of them exclusive.)

    Here's the coding:

    <SCRIPT LANGUAGE="JavaScript">

    function checkonly(0) {
    var form = document.fruitform;
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements.type == "checkbox") {
    if (form.elements.name != 0) {
    form.elements.checked = false;
    }
    }
    }
    }
    </SCRIPT>

    ....

    <FORM NAME="fruitform" ACTION="..." METHOD="POST">

    <input type="checkbox" value="1" name="fruit"
    onclick="checkonly('1')"> Apples

    <input type="checkbox" value="2" name="fruit"
    onclick="checkonly('2')"> Oranges

    <input type="checkbox" value="3" name="fruit"
    onclick="checkonly('3')"> Pears

    <input type="checkbox" value="0" name="fruit"
    onclick="checkonly('0')"> No Answer


    Again, to clarify my objective, I want users to be able to check
    either a combination of values 1-3, OR just be able to check 'No
    Answer' (which will then automatically uncheck any of the other
    boxes).

    Thanks again,
    J


    On Mon, 20 Sep 2004 10:48:11 GMT, "McKirahan" <>
    wrote:

    >"J Belly" <> wrote in message
    >news:...
    >> Hi, all:
    >>
    >> I'm hoping mine is a simple problem:
    >>
    >> Basically, I have 15 checkboxes, and one of them (default) is
    >> pre-checked when the page is loaded. If the user then selects any of
    >> the 14 other checkboxes listed, how do I get the default one to
    >> automatically uncheck? (To put it another way, if the user selects
    >> all 14 checkboxes and then changes his mind and selects the default
    >> one again, I want the other 14 to be de-selected.)
    >>
    >> Thanks for the help!
    >> J

    >
    >You could use radio buttons which automatically handle it but. like you, I
    >prefer the appearance of a checkbox. Try the following; watch for
    >word-wrap.
    >
    ><html>
    ><head>
    ><title>checkonly.htm</title>
    ><script type="text/javascript">
    >function checkonly(what) {
    > var form = document.form1;
    > for (var i=0; i<form.elements.length; i++) {
    > if (form.elements.type == "checkbox") {
    > if (form.elements.name != what) {
    > form.elements.checked = false;
    > }
    > }
    > }
    >}
    ></script>
    ></head>
    ><body>
    ><form name="form1" action="" method="post">
    ><input type="checkbox" name="x0" onclick="checkonly('x0')">
    ><input type="checkbox" name="x1" onclick="checkonly('x1')">
    ><input type="checkbox" name="x2" onclick="checkonly('x2')">
    ></form>
    ></body>
    ></html>
    >
    J Belly, Sep 20, 2004
    #3
  4. J Belly

    McKirahan Guest

    "J Belly" <> wrote in message
    news:...
    > McKirahan:
    >
    > Thanks for the help, but it didn't work. (By the way, the reason I
    > can't use radio buttons is because I want the user to be able to check
    > more than one box, while keeping just one of them exclusive.)
    >
    > Here's the coding:
    >
    > <SCRIPT LANGUAGE="JavaScript">
    >
    > function checkonly(0) {
    > var form = document.fruitform;
    > for (var i=0; i<form.elements.length; i++) {
    > if (form.elements.type == "checkbox") {
    > if (form.elements.name != 0) {
    > form.elements.checked = false;
    > }
    > }
    > }
    > }
    > </SCRIPT>
    >
    > ...
    >
    > <FORM NAME="fruitform" ACTION="..." METHOD="POST">
    >
    > <input type="checkbox" value="1" name="fruit"
    > onclick="checkonly('1')"> Apples
    >
    > <input type="checkbox" value="2" name="fruit"
    > onclick="checkonly('2')"> Oranges
    >
    > <input type="checkbox" value="3" name="fruit"
    > onclick="checkonly('3')"> Pears
    >
    > <input type="checkbox" value="0" name="fruit"
    > onclick="checkonly('0')"> No Answer
    >
    >
    > Again, to clarify my objective, I want users to be able to check
    > either a combination of values 1-3, OR just be able to check 'No
    > Answer' (which will then automatically uncheck any of the other
    > boxes).
    >
    > Thanks again,
    > J


    [snip]

    Tyr this:

    <html>
    <head>
    <title>uncheck.htm</title>
    <script type="text/javascript">
    function checkonly(what) {
    var form = document.fruitform;
    if (what == 0) {
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements.type == "checkbox") {
    if (form.elements.value != "0") {
    if (form.elements.name != what) {
    form.elements.checked = false;
    }
    }
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="fruitform" action="..." method="post">
    <input type="checkbox" value="1" name="fruit"
    onclick="checkonly('1')"> Apples
    <input type="checkbox" value="2" name="fruit"
    onclick="checkonly('2')"> Oranges
    <input type="checkbox" value="3" name="fruit"
    onclick="checkonly('3')"> Pears
    <input type="checkbox" value="0" name="fruit"
    onclick="checkonly('0')"> No Answer
    </form>
    </body>
    </html>
    McKirahan, Sep 20, 2004
    #4
  5. J Belly

    J Belly Guest

    McKirahan:

    Well, it's partly working...

    If any of the checkboxes (aside from 'No Answer') are checked, and
    then I check the 'No Answer' box, the other ones automatically uncheck
    (which is what I want).

    But if I first have the 'No Answer' box checked, and then I try
    selecting any of the other boxes, the 'No Answer' box still remains
    checked (whereas I need it to be unchecked).

    Any ideas?

    Thanks,
    J



    On Mon, 20 Sep 2004 20:05:31 GMT, "McKirahan" <>
    wrote:

    >[snip]
    >
    >Tyr this:
    >
    ><html>
    ><head>
    ><title>uncheck.htm</title>
    ><script type="text/javascript">
    >function checkonly(what) {
    > var form = document.fruitform;
    > if (what == 0) {
    > for (var i=0; i<form.elements.length; i++) {
    > if (form.elements.type == "checkbox") {
    > if (form.elements.value != "0") {
    > if (form.elements.name != what) {
    > form.elements.checked = false;
    > }
    > }
    > }
    > }
    > }
    >}
    ></script>
    ></head>
    ><body>
    ><form name="fruitform" action="..." method="post">
    ><input type="checkbox" value="1" name="fruit"
    > onclick="checkonly('1')"> Apples
    ><input type="checkbox" value="2" name="fruit"
    > onclick="checkonly('2')"> Oranges
    ><input type="checkbox" value="3" name="fruit"
    > onclick="checkonly('3')"> Pears
    ><input type="checkbox" value="0" name="fruit"
    > onclick="checkonly('0')"> No Answer
    ></form>
    ></body>
    ></html>
    >
    J Belly, Sep 20, 2004
    #5
  6. J Belly

    McKirahan Guest

    "J Belly" <> wrote in message
    news:...
    > McKirahan:
    >
    > Well, it's partly working...
    >
    > If any of the checkboxes (aside from 'No Answer') are checked, and
    > then I check the 'No Answer' box, the other ones automatically uncheck
    > (which is what I want).
    >
    > But if I first have the 'No Answer' box checked, and then I try
    > selecting any of the other boxes, the 'No Answer' box still remains
    > checked (whereas I need it to be unchecked).
    >
    > Any ideas?
    >
    > Thanks,
    > J


    [snip]

    Try this though I only tested it under IE.

    <html>
    <head>
    <title>unchecks.htm</title>
    <script type="text/javascript">
    function checkonly(what) {
    var form = document.fruitform;
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements.type == "checkbox") {
    if (what == 0) {
    if (form.elements.value != "0") {
    if (form.elements.name != what) {
    form.elements.checked = false;
    }
    }
    } else {
    if (form.elements.value == "0") {
    form.elements.checked = false;
    }
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="fruitform" action="..." method="post">
    <input type="checkbox" value="1" name="fruit" onclick="checkonly('1')">
    Apples
    <input type="checkbox" value="2" name="fruit" onclick="checkonly('2')">
    Oranges
    <input type="checkbox" value="3" name="fruit" onclick="checkonly('3')">
    Pears
    <input type="checkbox" value="0" name="fruit" onclick="checkonly('0')"> No
    Answer
    </form>
    </body>
    </html>
    McKirahan, Sep 21, 2004
    #6
  7. J Belly

    J Belly Guest

    It works!! But.....I've run into one tiny snag...

    Within the same form, I have 4 other sections/questions, each having a
    'No Answer' checkbox. So when I used your script, and tried selecting
    a 'No Answer' in one section, all the other selections I had made
    relating to the other questions got unchecked.

    Is there an easy way to fix (and still keep everything in one form)?

    Thank you VERY MUCH for the help so far,
    J


    On Tue, 21 Sep 2004 01:58:55 GMT, "McKirahan" <>
    wrote:

    >"J Belly" <> wrote in message
    >news:...
    >> McKirahan:
    >>
    >> Well, it's partly working...
    >>
    >> If any of the checkboxes (aside from 'No Answer') are checked, and
    >> then I check the 'No Answer' box, the other ones automatically uncheck
    >> (which is what I want).
    >>
    >> But if I first have the 'No Answer' box checked, and then I try
    >> selecting any of the other boxes, the 'No Answer' box still remains
    >> checked (whereas I need it to be unchecked).
    >>
    >> Any ideas?
    >>
    >> Thanks,
    >> J

    >
    >[snip]
    >
    >Try this though I only tested it under IE.
    >
    ><html>
    ><head>
    ><title>unchecks.htm</title>
    ><script type="text/javascript">
    >function checkonly(what) {
    > var form = document.fruitform;
    > for (var i=0; i<form.elements.length; i++) {
    > if (form.elements.type == "checkbox") {
    > if (what == 0) {
    > if (form.elements.value != "0") {
    > if (form.elements.name != what) {
    > form.elements.checked = false;
    > }
    > }
    > } else {
    > if (form.elements.value == "0") {
    > form.elements.checked = false;
    > }
    > }
    > }
    > }
    >}
    ></script>
    ></head>
    ><body>
    ><form name="fruitform" action="..." method="post">
    ><input type="checkbox" value="1" name="fruit" onclick="checkonly('1')">
    >Apples
    ><input type="checkbox" value="2" name="fruit" onclick="checkonly('2')">
    >Oranges
    ><input type="checkbox" value="3" name="fruit" onclick="checkonly('3')">
    >Pears
    ><input type="checkbox" value="0" name="fruit" onclick="checkonly('0')"> No
    >Answer
    ></form>
    ></body>
    ></html>
    >
    J Belly, Sep 21, 2004
    #7
  8. On Mon, 20 Sep 2004 22:24:16 -0700, J Belly <> wrote:

    [snip]

    > Within the same form, I have 4 other sections/questions, each having a
    > 'No Answer' checkbox. So when I used your script, and tried selecting a
    > 'No Answer' in one section, all the other selections I had made relating
    > to the other questions got unchecked.
    >
    > Is there an easy way to fix (and still keep everything in one form)?


    It depends on the structure of your form. Assuming that all checkboxes
    within a particular group share the same name, but these separate sections
    use different group names (see my example), there's no problem at all.

    <URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/belly/checkboxes.html>

    As I say in the comments, the current code assumes that the "No Answer"
    option is the first checkbox in each group. If you place it last, you need
    to uncomment the four lines prefixed with single-line comment delimiters
    (//) and delete (or comment-out) the line below each of them.

    The code is almost self-contained[1]. All you need to do is (repeating
    some of the above):

    1) Ensure that each checkbox within a group share exactly the same
    name. Related checkboxes should anyway, so this shouldn't be an
    issue.
    2) Ensure that no two groups use the same name. Again, this
    shouldn't be an issue.
    3) Add the intrinsic event listener

    onclick="setDefault(this);"

    to each checkbox. I would have preferred using the change event,
    but IE doesn't support that in the way I think it should.

    The page in the URL above has been tested with:

    IE6; Opera 7.54; Mozilla 1.0 and 1.73; Netscape 4.77 and 7.2;
    Mozilla Firefox 0.9.3

    and works as expected in all of them. I define "works" as:

    1) Selecting the first checkbox in each group should uncheck all
    other boxes in that group.
    2) Selecting any checkbox, other than the first, should ensure that
    the first box becomes unchecked.
    3) Multiple checkboxes, excluding the first, can be checked at the
    same time.

    The commented code behaves the same way, except "first" becomes "last" in
    the description above.

    The final point to note in all of this is to make sure that you check the
    values on the server. Don't rely solely on Javascript for validation if
    you can help it.

    [snipped top-post]

    Hope that helps,
    Mike


    [1] I'd call it truly self-contained if it added the necessary event
    handlers to the HTML itself, but that would be difficult without seeing
    your page.

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Sep 21, 2004
    #8
  9. J Belly

    McKirahan Guest

    "J Belly" <> wrote in message
    news:eek:...
    > It works!! But.....I've run into one tiny snag...
    >
    > Within the same form, I have 4 other sections/questions, each having a
    > 'No Answer' checkbox. So when I used your script, and tried selecting
    > a 'No Answer' in one section, all the other selections I had made
    > relating to the other questions got unchecked.
    >
    > Is there an easy way to fix (and still keep everything in one form)?
    >
    > Thank you VERY MUCH for the help so far,
    > J


    [snip]

    <html>
    <head>
    <title>uncheckz.htm</title>
    <script type="text/javascript">
    function checkonly(ques,what) {
    var form = document.fruitform;
    for (var i=0; i<form.elements.length; i++) {
    if (form.elements.type == "checkbox") {
    if (form.elements.name == ques) {
    if (what == 0) {
    if (form.elements.value != "0") {
    if (form.elements.name != what) {
    form.elements.checked = false;
    }
    }
    } else {
    if (form.elements.value == "0") {
    form.elements.checked = false;
    }
    }
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="fruitform" action="..." method="post">
    <input type="checkbox" value="1" name="fruit1"
    onclick="checkonly('fruit1',1)"> Apples
    <input type="checkbox" value="2" name="fruit1"
    onclick="checkonly('fruit1',2)"> Oranges
    <input type="checkbox" value="3" name="fruit1"
    onclick="checkonly('fruit1',3)"> Pears
    <input type="checkbox" value="0" name="fruit1"
    onclick="checkonly('fruit1',0)"> No Answer
    <br>
    <input type="checkbox" value="1" name="fruit2"
    onclick="checkonly('fruit2',1)"> Apples
    <input type="checkbox" value="2" name="fruit2"
    onclick="checkonly('fruit2',2)"> Oranges
    <input type="checkbox" value="3" name="fruit2"
    onclick="checkonly('fruit2',3)"> Pears
    <input type="checkbox" value="0" name="fruit2"
    onclick="checkonly('fruit2',0)"> No Answer
    <br>
    <input type="checkbox" value="1" name="fruit3"
    onclick="checkonly('fruit3',1)"> Apples
    <input type="checkbox" value="2" name="fruit3"
    onclick="checkonly('fruit3',2)"> Oranges
    <input type="checkbox" value="3" name="fruit3"
    onclick="checkonly('fruit3',3)"> Pears
    <input type="checkbox" value="0" name="fruit3"
    onclick="checkonly('fruit3',0)"> No Answer
    <br>
    <input type="checkbox" value="1" name="fruit4"
    onclick="checkonly('fruit4',1)"> Apples
    <input type="checkbox" value="2" name="fruit4"
    onclick="checkonly('fruit4',2)"> Oranges
    <input type="checkbox" value="3" name="fruit4"
    onclick="checkonly('fruit4',3)"> Pears
    <input type="checkbox" value="0" name="fruit4"
    onclick="checkonly('fruit4',0)"> No Answer
    <br>
    <input type="checkbox" value="1" name="fruit5"
    onclick="checkonly('fruit5',1)"> Apples
    <input type="checkbox" value="2" name="fruit5"
    onclick="checkonly('fruit5',2)"> Oranges
    <input type="checkbox" value="3" name="fruit5"
    onclick="checkonly('fruit5',3)"> Pears
    <input type="checkbox" value="0" name="fruit5"
    onclick="checkonly('fruit5',0)"> No Answer
    </form>
    </body>
    </html>
    McKirahan, Sep 21, 2004
    #9
  10. Simple , indeed in you HTML code where you have a "checkbox" button put a
    "radio" button instead.
    That option let you select only one of the fifhteen items .
    "J Belly" <> escreveu na mensagem
    news:...
    > Hi, all:
    >
    > I'm hoping mine is a simple problem:
    >
    > Basically, I have 15 checkboxes, and one of them (default) is
    > pre-checked when the page is loaded. If the user then selects any of
    > the 14 other checkboxes listed, how do I get the default one to
    > automatically uncheck? (To put it another way, if the user selects
    > all 14 checkboxes and then changes his mind and selects the default
    > one again, I want the other 14 to be de-selected.)
    >
    > Thanks for the help!
    > J
    >
    >
    Oscar Monteiro, Sep 21, 2004
    #10
  11. J Belly

    McKirahan Guest

    "Oscar Monteiro" <> wrote in message
    news:41502186$0$4897$...
    > Simple , indeed in you HTML code where you have a "checkbox" button put a
    > "radio" button instead.
    > That option let you select only one of the fifhteen items .


    "(By the way, the reason I can't use radio buttons is because I want the
    user to be able to check
    more than one box, while keeping just one of them exclusive.)"

    -- J Belly (Monday, September 20, 2004 2:36 PM)
    McKirahan, Sep 21, 2004
    #11
  12. J Belly

    J Belly Guest

    McKirahan:

    THAT WORKS PERFECTLY!!! Thank you! Thank you! Thank you!

    I just really can't thank you enough for all your help. My knowledge
    of javascript is pretty much just cut-n-paste, so I was almost gonna
    give up on offering this 'No Answer' feature because it just seemed
    way too complicated to me. But now my Web page seems totally
    professional :)

    Again, THANK YOU VERY MUCH for sticking with me and giving me this
    perfect solution. I truly appreciate it!

    Take care,
    J



    On Tue, 21 Sep 2004 10:14:49 GMT, "McKirahan" <>
    wrote:

    >"J Belly" <> wrote in message
    >news:eek:...
    >> It works!! But.....I've run into one tiny snag...
    >>
    >> Within the same form, I have 4 other sections/questions, each having a
    >> 'No Answer' checkbox. So when I used your script, and tried selecting
    >> a 'No Answer' in one section, all the other selections I had made
    >> relating to the other questions got unchecked.
    >>
    >> Is there an easy way to fix (and still keep everything in one form)?
    >>
    >> Thank you VERY MUCH for the help so far,
    >> J

    >
    >[snip]
    >
    ><html>
    ><head>
    ><title>uncheckz.htm</title>
    ><script type="text/javascript">
    >function checkonly(ques,what) {
    > var form = document.fruitform;
    > for (var i=0; i<form.elements.length; i++) {
    > if (form.elements.type == "checkbox") {
    > if (form.elements.name == ques) {
    > if (what == 0) {
    > if (form.elements.value != "0") {
    > if (form.elements.name != what) {
    > form.elements.checked = false;
    > }
    > }
    > } else {
    > if (form.elements.value == "0") {
    > form.elements.checked = false;
    > }
    > }
    > }
    > }
    > }
    >}
    ></script>
    ></head>
    ><body>
    ><form name="fruitform" action="..." method="post">
    ><input type="checkbox" value="1" name="fruit1"
    >onclick="checkonly('fruit1',1)"> Apples
    ><input type="checkbox" value="2" name="fruit1"
    >onclick="checkonly('fruit1',2)"> Oranges
    ><input type="checkbox" value="3" name="fruit1"
    >onclick="checkonly('fruit1',3)"> Pears
    ><input type="checkbox" value="0" name="fruit1"
    >onclick="checkonly('fruit1',0)"> No Answer
    ><br>
    ><input type="checkbox" value="1" name="fruit2"
    >onclick="checkonly('fruit2',1)"> Apples
    ><input type="checkbox" value="2" name="fruit2"
    >onclick="checkonly('fruit2',2)"> Oranges
    ><input type="checkbox" value="3" name="fruit2"
    >onclick="checkonly('fruit2',3)"> Pears
    ><input type="checkbox" value="0" name="fruit2"
    >onclick="checkonly('fruit2',0)"> No Answer
    ><br>
    ><input type="checkbox" value="1" name="fruit3"
    >onclick="checkonly('fruit3',1)"> Apples
    ><input type="checkbox" value="2" name="fruit3"
    >onclick="checkonly('fruit3',2)"> Oranges
    ><input type="checkbox" value="3" name="fruit3"
    >onclick="checkonly('fruit3',3)"> Pears
    ><input type="checkbox" value="0" name="fruit3"
    >onclick="checkonly('fruit3',0)"> No Answer
    ><br>
    ><input type="checkbox" value="1" name="fruit4"
    >onclick="checkonly('fruit4',1)"> Apples
    ><input type="checkbox" value="2" name="fruit4"
    >onclick="checkonly('fruit4',2)"> Oranges
    ><input type="checkbox" value="3" name="fruit4"
    >onclick="checkonly('fruit4',3)"> Pears
    ><input type="checkbox" value="0" name="fruit4"
    >onclick="checkonly('fruit4',0)"> No Answer
    ><br>
    ><input type="checkbox" value="1" name="fruit5"
    >onclick="checkonly('fruit5',1)"> Apples
    ><input type="checkbox" value="2" name="fruit5"
    >onclick="checkonly('fruit5',2)"> Oranges
    ><input type="checkbox" value="3" name="fruit5"
    >onclick="checkonly('fruit5',3)"> Pears
    ><input type="checkbox" value="0" name="fruit5"
    >onclick="checkonly('fruit5',0)"> No Answer
    ></form>
    ></body>
    ></html>
    >
    J Belly, Sep 22, 2004
    #12
  13. J Belly

    J Belly Guest

    Michael:

    Thank you for your detailed explanation and the great URL example.

    I was able to use the other poster's (McKirahan's) script, and it
    works perfectly for me.

    A moot point, but just to let you know: the placement of my 'No
    Answer' box is somewhat disorganized because in one section, it's
    listed at the end, and in others, it's listed second or third from the
    end (actually, I'm using multiple rows and columns, and because each
    checkbox has its own cell, I'm not really sure where the "end" is).

    I appreciate your advice about not relying on javascript as the only
    form of validation. I wasn't sure if I should spend the time to code
    a server-side validation, but I guess it wouldn't hurt.

    Thanks again for your help.

    J



    On Tue, 21 Sep 2004 07:02:53 GMT, "Michael Winter"
    <> wrote:

    >On Mon, 20 Sep 2004 22:24:16 -0700, J Belly <> wrote:
    >
    >[snip]
    >
    >> Within the same form, I have 4 other sections/questions, each having a
    >> 'No Answer' checkbox. So when I used your script, and tried selecting a
    >> 'No Answer' in one section, all the other selections I had made relating
    >> to the other questions got unchecked.
    >>
    >> Is there an easy way to fix (and still keep everything in one form)?

    >
    >It depends on the structure of your form. Assuming that all checkboxes
    >within a particular group share the same name, but these separate sections
    >use different group names (see my example), there's no problem at all.
    >
    ><URL:http://www.mlwinter.pwp.blueyonder.co.uk/clj/belly/checkboxes.html>
    >
    >As I say in the comments, the current code assumes that the "No Answer"
    >option is the first checkbox in each group. If you place it last, you need
    >to uncomment the four lines prefixed with single-line comment delimiters
    >(//) and delete (or comment-out) the line below each of them.
    >
    >The code is almost self-contained[1]. All you need to do is (repeating
    >some of the above):
    >
    >1) Ensure that each checkbox within a group share exactly the same
    > name. Related checkboxes should anyway, so this shouldn't be an
    > issue.
    >2) Ensure that no two groups use the same name. Again, this
    > shouldn't be an issue.
    >3) Add the intrinsic event listener
    >
    > onclick="setDefault(this);"
    >
    > to each checkbox. I would have preferred using the change event,
    > but IE doesn't support that in the way I think it should.
    >
    >The page in the URL above has been tested with:
    >
    > IE6; Opera 7.54; Mozilla 1.0 and 1.73; Netscape 4.77 and 7.2;
    > Mozilla Firefox 0.9.3
    >
    >and works as expected in all of them. I define "works" as:
    >
    >1) Selecting the first checkbox in each group should uncheck all
    > other boxes in that group.
    >2) Selecting any checkbox, other than the first, should ensure that
    > the first box becomes unchecked.
    >3) Multiple checkboxes, excluding the first, can be checked at the
    > same time.
    >
    >The commented code behaves the same way, except "first" becomes "last" in
    >the description above.
    >
    >The final point to note in all of this is to make sure that you check the
    >values on the server. Don't rely solely on Javascript for validation if
    >you can help it.
    >
    >[snipped top-post]
    >
    >Hope that helps,
    >Mike
    >
    >
    >[1] I'd call it truly self-contained if it added the necessary event
    >handlers to the HTML itself, but that would be difficult without seeing
    >your page.
    J Belly, Sep 22, 2004
    #13
  14. Use the CheckBoxList control.

    with regards,


    J.V.Ravichandran
    - http://www.geocities.com/
    jvravichandran
    - http://www.411asp.net/func/search?
    qry=Ravichandran+J.V.&cob=aspnetpro
    - http://www.southasianoutlook.com
    - http://www.MSDNAA.Net
    - http://www.csharphelp.com
    - http://www.poetry.com/Publications/
    display.asp?ID=P3966388&BN=999&PN=2
    - Or, just search on "J.V.Ravichandran"
    at http://www.Google.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Ravichandran J.V., Sep 22, 2004
    #14
  15. J Belly

    McKirahan Guest

    "Ravichandran J.V." <> wrote in message
    news:41513f39$0$26163$...
    > Use the CheckBoxList control.
    >
    > with regards,
    >
    >
    > J.V.Ravichandran


    CheckBoxList Control
    http://www.w3schools.com/aspnet/control_checkboxlist.asp
    The CheckBoxList control is used to create a multi-selection check box
    group.


    ..NET Framework Class Library
    CheckBoxList Class
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemwebuiwebcontrolscheckboxlistclasstopic.asp


    Using ASP.NET CheckBoxList Control
    http://www.c-sharpcorner.com/asp/Controls/CheckBoxListControlPSD.asp
    McKirahan, Sep 22, 2004
    #15
    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. Joergen Bech
    Replies:
    0
    Views:
    540
    Joergen Bech
    Jul 27, 2005
  2. Rodney King

    Desperately: Need help selecting a checkbox

    Rodney King, Oct 12, 2004, in forum: ASP General
    Replies:
    7
    Views:
    172
    Rodney King
    Oct 13, 2004
  3. Eric
    Replies:
    1
    Views:
    156
  4. Phil Powell
    Replies:
    7
    Views:
    210
    The Nordic One
    Apr 14, 2004
  5. Replies:
    3
    Views:
    123
Loading...

Share This Page