Selecting/deselecting checkboxes

Discussion in 'Javascript' started by Jez, Jul 17, 2003.

  1. Jez

    Jez Guest

    Hi,

    I have created the following functions to select and deselect
    checkboxes in a form ...

    function check(checkbox) {
    for (i = 0; i < checkbox.length; i++) {
    checkbox.checked = true;
    }
    return "Select all";
    }

    function uncheck(checkbox) {
    for (i = 0; i < checkbox.length; i++) {
    checkbox.checked = false;
    }
    return "Select none";
    }

    The form itself looks like this ...

    <form name="form" action="delete.php" method="post">
    <input type="checkbox" name="id" value="1" />Option 1<br />
    <input type="checkbox" name="id" value="2" />Option 2<br />
    <input type="checkbox" name="id" value="3" />Option 3<br />
    <input type="button" value="Select all"
    onClick="this.value=check(this.form.id)" />
    <input type="button" value="Select none"
    onClick="this.value=uncheck(this.form.id)" />
    <input type="submit" name="submit" value="Delete selected" />
    </form>

    This all seems to work very well (although constructive critisism
    would be welcome), however I actually need to name the input 'id[]'
    because I'm generating an array of values. Unfortunately the funtions
    don't seem to like '[]'. Is there anything I can do?

    Thank you in advance for your help. It's greatly appreciated!

    Jez
     
    Jez, Jul 17, 2003
    #1
    1. Advertisements

  2. Feeding an array of checkboxes into a php script?

    <input type="checkbox" name="id[]" id="id" value="1">
    <input type="checkbox" name="id[]" id="id" value="2">
    <input type="checkbox" name="id[]" id="id" value="3">

    or try:

    <input type="button" value="Select all"
    onClick="this.value=check(this.form.elements['id[]'])" />
    <input type="button" value="Select none"
    onClick="this.value=uncheck(this.form.elements['id[]'])" />

    "Jez" <> wrote in message
    news:...
    > Hi,
    >
    > I have created the following functions to select and deselect
    > checkboxes in a form ...
    >
    > function check(checkbox) {
    > for (i = 0; i < checkbox.length; i++) {
    > checkbox.checked = true;
    > }
    > return "Select all";
    > }
    >
    > function uncheck(checkbox) {
    > for (i = 0; i < checkbox.length; i++) {
    > checkbox.checked = false;
    > }
    > return "Select none";
    > }
    >
    > The form itself looks like this ...
    >
    > <form name="form" action="delete.php" method="post">
    > <input type="checkbox" name="id" value="1" />Option 1<br />
    > <input type="checkbox" name="id" value="2" />Option 2<br />
    > <input type="checkbox" name="id" value="3" />Option 3<br />
    > <input type="button" value="Select all"
    > onClick="this.value=check(this.form.id)" />
    > <input type="button" value="Select none"
    > onClick="this.value=uncheck(this.form.id)" />
    > <input type="submit" name="submit" value="Delete selected" />
    > </form>
    >
    > This all seems to work very well (although constructive critisism
    > would be welcome), however I actually need to name the input 'id[]'
    > because I'm generating an array of values. Unfortunately the funtions
    > don't seem to like '[]'. Is there anything I can do?
    >
    > Thank you in advance for your help. It's greatly appreciated!
    >
    > Jez
     
    Richard Hockey, Jul 17, 2003
    #2
    1. Advertisements

  3. Jez

    Jez Hailwood Guest

    Thanks, that's excellent!

    My function now works perfectly unless there's only one checkbox, in
    which case it doesn't work at all.

    Any ideas?

    Jez

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Jez Hailwood, Jul 17, 2003
    #3
  4. A single checkbox doesn't have a length property, so you can't loop through
    using id.length, you have to specifically check the id

    An example I pulled from a page which dynamically generates a group of
    checkboxes:

    // check for single checkbox by seeing if an array has been created
    var cblength=document.forms['inbox'].elements['del'].length;
    if(typeof cblength=="undefined")
    {
    if(document.forms['inbox'].elements['del'].checked==true) count++;
    }
    else
    {
    for(i=0;i<document.forms['inbox'].elements['del'].length;i++)
    {
    if(document.forms['inbox'].elements['del'].checked) count++;
    }
    }


    Assuming you are using php

    <input type="checkbox" name="bob" value="3">

    and in the php

    <?php
    if(isset(_POST["bob"])
    {
    // checkbox 'bob' has been checked in the form
    $bob=$_POST["bob"];
    // variable $bob will be set to the value of checkbox 'bob', in this case
    "3"
    }
    else
    {
    // checkbox 'bob' has not been checked, set a default value
    $bob="0";
    }

    "Jez Hailwood" <> wrote in message
    news:3f171020$0$203$...
    > Thanks, that's excellent!
    >
    > My function now works perfectly unless there's only one checkbox, in
    > which case it doesn't work at all.
    >
    > Any ideas?
    >
    > Jez
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Richard Hockey, Jul 18, 2003
    #4
  5. Jez

    Jez Guest

    Problem now solved with a much simpler solution ...

    function selectAll(state) {
    for (i = 0; i < document.form.elements.length; i++) {
    var checkbox = document.form.elements;
    checkbox.checked = state;
    }
    }

    <input type="button" value="Select all" onClick="selectAll(true);" />
    <input type="button" value="Clear all" onClick="selectAll(false);" />

    Jez


    Jez Hailwood <> wrote in message news:<3f171020$0$203$>...
    > Thanks, that's excellent!
    >
    > My function now works perfectly unless there's only one checkbox, in
    > which case it doesn't work at all.
    >
    > Any ideas?
    >
    > Jez
     
    Jez, Jul 18, 2003
    #5
    1. Advertisements

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. Fresh Air Rider

    Checkboxes with Repeaters and DataGrids

    Fresh Air Rider, Mar 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    595
    Fresh Air Rider
    Mar 5, 2004
  2. kannadasan
    Replies:
    1
    Views:
    1,125
  3. Mike C. Fletcher
    Replies:
    1
    Views:
    341
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 5, 2004
  4. Mark B
    Replies:
    3
    Views:
    3,032
    Mark B
    Mar 30, 2009
  5. Rick C

    Selecting multiple CheckBoxes in a CheckBoxList

    Rick C, Jun 29, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    427
    Rick C
    Jun 29, 2004
  6. Eric
    Replies:
    1
    Views:
    218
  7. Replies:
    5
    Views:
    353
  8. JL
    Replies:
    7
    Views:
    318
    Thomas 'PointedEars' Lahn
    Mar 10, 2008
Loading...