Finding out how many checkboxes are in a table

Discussion in 'Javascript' started by stevewy@hotmail.com, Oct 7, 2008.

  1. Guest

    I'm looking to manipulate/check the state of various checkboxes and
    radios in a form. Because I don't want to access all of the
    checkboxes in the form, only a specific section, I am trying to narrow
    down the list of elements to check. I thought I could place those
    elements within a named DIV or a table, and then reference that DIV or
    table by its ID. But it's not working.

    Say for instance I wanted Javascript to issue an alert (just for
    testing purposes), giving me the number of elements in a Table. If
    the Table's ID was "fred", why does the instruction below not work?

    alert(document.getElementById('fred').elements.length)

    It comes up with an error saying it's "Null or not an object". Any
    ideas? Is this just not possible, or am I doing it wrong?

    If I use the name of the form (which is called "form" as well), to do:

    alert(document.form.getElementById('fred').elements.length)

    .... it says the Object doesn't support this property or method.


    Steve Wylie
    , Oct 7, 2008
    #1
    1. Advertising

  2. Laser Lips Guest

    On Oct 7, 11:26 am, wrote:
    > I'm looking to manipulate/check the state of various checkboxes and
    > radios in a form.  Because I don't want to access all of the
    > checkboxes in the form, only a specific section, I am trying to narrow
    > down the list of elements to check.  I thought I could place those
    > elements within a named DIV or a table, and then reference that DIV or
    > table by its ID.  But it's not working.
    >
    > Say for instance I wanted Javascript to issue an alert (just for
    > testing purposes), giving me the number of elements in a Table.  If
    > the Table's ID was "fred", why does the instruction below not work?
    >
    > alert(document.getElementById('fred').elements.length)
    >
    > It comes up with an error saying it's "Null or not an object".  Any
    > ideas?  Is this just not possible, or am I doing it wrong?
    >
    > If I use the name of the form (which is called "form" as well), to do:
    >
    > alert(document.form.getElementById('fred').elements.length)
    >
    > ... it says the Object doesn't support this property or method.
    >
    > Steve Wylie



    Because document.getElementById('fred').elements is not an valid
    object, where did you get that from?

    you can't just make things up as you go along

    Use ...

    tab = document.getElementById("fred");
    inputs=tab.getElementsByTagName("input");
    numOfCB=0;
    for(x=0;x<inputs.length;x++)
    {
    if(inputs[x].type=="checkbox")numOfCB++;
    }
    alert(numOfCB);
    Laser Lips, Oct 7, 2008
    #2
    1. Advertising

  3. Guest

    I based my (failed) example on changing a working example I got from
    the internet, which was used for something else. Clearly it doesn't
    translate to work in this case!

    You provided an answer to the example I gave about finding how many
    checkboxes there are in the Table "Fred", but unfortunately this is
    not actually what I am trying to achieve, I just gave that as an
    example to try to understand how I am meant to properly deal with
    elements in a certain part of a form. I'll try and explain it better:

    If I wanted to deal with all the elements in a form (called "form"), I
    could use "alert(form.elements.length)", which would tell me how many
    elements there were in the entire form. So if I wanted to find out if
    a certain checkbox was ticked, and I knew the element number of that
    checkbox, I could address it directly with form.elements[47]. At
    least I think this is how it works.

    What I was trying to do, if it was possible in Javascript, was to
    access just the form elements that were present in a named table. I
    am imagining here that the first form element in the table would be
    element [0]. Or is what I want totally not possible in Javascript,
    and the form elements have to be referenced across the entire form?

    I mean, if I had a Table with an ID of "Fred", can I reference certain
    properties of the table by using "fred.property"?

    It is apparent I have little understanding of the way this is supposed
    to work, so any advice you can give me would be appreciated. I did
    try Googling for a solution, but could not find anything that told me
    what I needed to know...

    Steve
    , Oct 7, 2008
    #3
  4. meinte:

    [snip]

    Only forms have an elements collection.

    > It is apparent I have little understanding of the way this is supposed
    > to work, so any advice you can give me would be appreciated.




    Once more:

    var table = document.getElementById("myTable");
    var inputs = table.getElementsByTagName("input");
    var selects = table.getElementsByTagName("select");
    var textareas = table.getElementsByTagName("textarea");
    ....

    Either combine those collections or iterate through each of them
    seperately. Whether you have a form somewhere is in this case completely
    irrelevant.

    Gregor



    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Oct 7, 2008
    #4
  5. Guest

    Okay, thanks for the info. And back to the drawing board I go...

    Steve
    , Oct 7, 2008
    #5
  6. Laser Lips Guest

    On Oct 7, 1:35 pm, wrote:
    > Okay, thanks for the info.  And back to the drawing board I go...
    >
    > Steve


    ???
    Laser Lips, Oct 7, 2008
    #6
  7. Guest

    What's with the "???" Laserlips? You have a query with what I said?

    Steve
    , Oct 8, 2008
    #7
  8. Guest

    Ah, sorry. Looked again at both your responses and figured out that
    you had both answered my question. Once I'd worked out what you were
    getting at. So if I wanted to isolate just the checkboxes in an area
    of my document called "q2", and check the last one of them, following
    your instructions above I could do:

    function tickq2(){
    tab = document.getElementById("q2");
    cbs=tab.getElementsByTagName("input");
    cbs[cbs.length-1].checked=1
    }

    Actually, it wouldn't isolate just the checkboxes in that section of
    the document, but all of the input tags whatever they were; but I
    could certainly work around that.

    So, thank you for the assistance, Gregor and Laser Lips.
    , Oct 8, 2008
    #8
    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. Marco Ippolito
    Replies:
    0
    Views:
    2,561
    Marco Ippolito
    Oct 11, 2004
  2. Andy B
    Replies:
    2
    Views:
    461
    Andy B
    Nov 20, 2008
  3. ngw
    Replies:
    2
    Views:
    117
    David Vallner
    Feb 19, 2006
  4. Jim Thomason
    Replies:
    3
    Views:
    151
    Topmind
    Oct 2, 2004
  5. Mark
    Replies:
    1
    Views:
    56
Loading...

Share This Page