counting checkboxes?

Discussion in 'Javascript' started by ASM, Jul 19, 2007.

  1. ASM

    ASM Guest

    En réponse à Tamer Higazi qui nous a susurré, en date du : 20/07/07
    1:45, le message sibyllin suivant :
    > Hi!
    > I have a form where I have embedded a table. Inside a column in each row
    > is a checkbox. I want:
    >
    > A) to get the values of the checkboxes
    > B) to count the total amount of checkboxes (which are selected)
    >
    > If somebody can give me an advise, I would thank him7her very much.


    I think you have to do a loop on your checboxes

    functions checks() {
    var f = document.forms[0], c=0, ch=new Array();
    for(var i=0; i<f.length; i++) {
    if(f.type=='checkbox') {
    ch[ch.length] = f.value;
    if(f.checked) c++;
    }
    }
    alert('checked checboxes = '+c+'\nvalues = '+ch);
    }

    --
    Stephane Moriaux et son (moins) vieux Mac
     
    ASM, Jul 19, 2007
    #1
    1. Advertising

  2. ASM

    Evertjan. Guest

    Tamer Higazi wrote on 20 jul 2007 in comp.lang.javascript:

    > Hi!
    > I have a form where I have embedded a table. Inside a column in each
    > row is a checkbox. I want:
    >
    > A) to get the values of the checkboxes


    Checkboxes have no value, methinks.

    > B) to count the total amount of checkboxes (which are selected)
    >
    > If somebody can give me an advise, I would thank him7her very much.


    As you probably expected, this can easily be done with Javascript.

    The collection of inputs can be made available here:

    var v = document.forms['myForm'].elements

    Then you can look at each input and if the checked is true,
    [it must be a checkbox, AND checkedm
    increment the counter.

    Try:

    var v = document.forms['myForm'].elements
    var n=0
    for(i=0;i<v.length;i++)
    if (v.checked)
    n++


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jul 19, 2007
    #2
    1. Advertising

  3. ASM

    Guest

    On Jul 19, 4:45 pm, Tamer Higazi <> wrote:
    > Hi!
    > I have a form where I have embedded a table. Inside a column in each row
    > is a checkbox. I want:
    >
    > A) to get the values of the checkboxes
    > B) to count the total amount of checkboxes (which are selected)


    You can access all elements of a form by using the form.elements
    collection. Then iterate through each and check that the current
    element has the type "checkbox". The value of the checkbox element is
    stored in the "checked" attribute. So try something like this:

    var count = 0;
    function foo() {
    var form = document.getElementById('someForm');
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++) {
    if (elements.type == 'checkbox') {
    var value = elements.checked;
    // do something with the value
    count++;
    }
    }
    }
     
    , Jul 20, 2007
    #3
  4. ASM

    Tamer Higazi Guest

    Hi!
    I have a form where I have embedded a table. Inside a column in each row
    is a checkbox. I want:

    A) to get the values of the checkboxes
    B) to count the total amount of checkboxes (which are selected)

    If somebody can give me an advise, I would thank him7her very much.


    with kind regards


    Tamer Higazi
     
    Tamer Higazi, Jul 20, 2007
    #4
  5. ASM

    RobG Guest

    On Jul 20, 7:32 am, "Evertjan." <> wrote:
    > Tamer Higazi wrote on 20 jul 2007 in comp.lang.javascript:
    >
    > > Hi!
    > > I have a form where I have embedded a table. Inside a column in each
    > > row is a checkbox. I want:

    >
    > > A) to get the values of the checkboxes

    >
    > Checkboxes have no value, methinks.


    The value attribute is mandatory for radio buttons and checkboxes in
    valid HTML:

    <URL: http://www.w3.org/TR/html4/interact/forms.html#adef-value-INPUT
    >



    --
    Rob
     
    RobG, Jul 20, 2007
    #5
  6. ASM

    Evertjan. Guest

    Kevin Scholl wrote on 20 jul 2007 in comp.lang.javascript:

    > Evertjan. wrote:
    >> Tamer Higazi wrote on 20 jul 2007 in comp.lang.javascript:
    >>
    >>> Hi!
    >>> I have a form where I have embedded a table. Inside a column in each
    >>> row is a checkbox. I want:
    >>>
    >>> A) to get the values of the checkboxes

    >>
    >> Checkboxes have no value, methinks.

    >
    > Eh? A checkbox indeed does have a value; it is what is passed upon
    > form submission if the checkbox is checked.
    >
    > [input name="elemName" id="elemName" type="checkbox" value="Yes" /]


    Mmmm... Yes.


    >>> B) to count the total amount of checkboxes (which are selected)
    >>>
    >>> If somebody can give me an advise, I would thank him7her very much.

    >>
    >> As you probably expected, this can easily be done with Javascript.
    >>
    >> The collection of inputs can be made available here:
    >>
    >> var v = document.forms['myForm'].elements
    >>
    >> Then you can look at each input and if the checked is true,
    >> [it must be a checkbox, AND checkedm

    >
    > Why must it be a checkbox? Radio buttons also have a "checked"
    > attribute.
    >
    >> increment the counter.
    >>
    >> Try:
    >>
    >> var v = document.forms['myForm'].elements
    >> var n=0
    >> for(i=0;i<v.length;i++)
    >> if (v.checked)

    >
    > if (v.type == "checkbox" && v.checked)


    Much better!


    >
    >> n++

    >
    >
    >




    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jul 20, 2007
    #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. Fresh Air Rider

    Checkboxes with Repeaters and DataGrids

    Fresh Air Rider, Mar 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    489
    Fresh Air Rider
    Mar 5, 2004
  2. kannadasan
    Replies:
    1
    Views:
    1,015
  3. Counting CheckBoxes

    , Feb 20, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    360
    Cowboy \(Gregory A. Beamer\)
    Feb 20, 2007
  4. trettr

    Counting checked checkboxes in a form

    trettr, Sep 19, 2003, in forum: Javascript
    Replies:
    1
    Views:
    76
    Martin Honnen
    Sep 19, 2003
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    204
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page