SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict rabbitdog's Avatar
    Join Date
    Jul 2001
    Location
    So. Tenn.
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Checking all checkboxes

    Hi,

    I have some check boxes dynamically generated from php, and I'd like to be able to have a link to "select them all".

    This is what I have so far:

    within my body tag:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
    field[i].checked = true ;
    }
    // End -->
    </script>

    the form name is "categories" and the checkboxes look like this:

    <input type=\"checkbox\" name=\"cat[]\" value=\"$row[catid]\" class=\"form\">

    and my link to select them all look like this:

    <a href=\"#\" onClick=\"checkAll('document.categories.cat[]')\">Select All</a>

    right now, whenever I click the link, I get the error 'undefined' is null or not an object.

    Any clues what's wrong?

    TYIA

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As a matter of fact I have a script to do just this: http://anarchos.xs.mw/formtricks.phtml
    ck :: bringing chris to the masses.

  3. #3
    SitePoint Addict rabbitdog's Avatar
    Join Date
    Jul 2001
    Location
    So. Tenn.
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool..thanks, just tried it.

    Any clue why I'd be getting an error: "'form.elements' is null or not an object" ?

    TYIA

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's probably because [] signifies an array in javascript.
    ck :: bringing chris to the masses.

  5. #5
    SitePoint Addict rabbitdog's Avatar
    Join Date
    Jul 2001
    Location
    So. Tenn.
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    know of any way around it from within javascript?

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi rabbitdog,

    The following error:
    .elements' is null or not an object"
    implies you are either calling the DOM before the html has been interpreted or you have a bad DOM (eg, trying to access a layer with improper references). To know exactly why you are getting the message, I/we would have to see the relevant code (the function & the pertinent html).

    Re the code you posted previously, why are including braces after 'cat'?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He did post the code at the top; cat[] is used to input it as an array in php. I don't know if there's a way around it other than the method my script uses: looping through all elements and checking if the name contains a certain string.
    ck :: bringing chris to the masses.

  8. #8
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Anachros,

    I agree with the looping through the form (I do that in both of the checkbox script/tutorials at my site, one of which also does selectInverse), but I still don't really understand the need for the braces in the html. Why aren't the fields being called cat1...catn, and if they must physically be in an array for php, then call a function with the onClick that assigns them so? sort of:

    var chkArray = new Array();
    var ndx=0;
    for (i=0;i < formObj.length; i++)
    {

    if (formObj.elements[i].type == 'checkbox') //&&form...[i].name.substr(x,y)=='something'
    {
    chkArray[ndx] = formObj.elements[i].value;
    ndx++;
    }
    }

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •