multiple checkboxes help needed

Discussion in 'Javascript' started by Ralph Freshour, Dec 14, 2003.

  1. I have multiple checkbox's created with an array name because I have
    many on the same web page - their names are like:

    frm_chk_delete[0]
    frm_chk_delete[1]
    frm_chk_delete[2]
    frm_chk_delete[3]
    etc.

    Here is my code line that creates each checkbox (the php variable get
    incremented of course):

    <INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
    $php_delete_counter;?>]">Delete

    When I click on the Delete button I want to call a JS routine to check
    that at least one checknbox has been checked and marked for deletion
    when the form submit is called my php file will detect and delete the
    checkboxes accordingly.

    I'd like to use JS before the submit and make sure at least one of the
    checkboxes is checked - how do I loop through these array name
    checkboxes in javascript to check their states before submitting?

    Thanks...
     
    Ralph Freshour, Dec 14, 2003
    #1
    1. Advertising

  2. Ralph Freshour <> writes:

    > frm_chk_delete[0]
    > frm_chk_delete[1]
    > frm_chk_delete[2]
    > frm_chk_delete[3]

    ....
    > <INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
    > $php_delete_counter;?>]">Delete

    ....
    > I'd like to use JS before the submit and make sure at least one of the
    > checkboxes is checked - how do I loop through these array name
    > checkboxes in javascript to check their states before submitting?


    ---
    <script type="text/javascript">
    function isOneSet(form) {
    for (var i=0;i<numberOfCheckboxes;i++) {
    if (form.elements["frm_chk_delete["+i+"]"].checked) {
    return true;
    }
    }
    return false;
    }
    </script>
    ---
    and
    ---
    <form ... onsubmit="if (!isOneSet(this)) {alert('SET ONE!');return false;}">
    ---

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Dec 14, 2003
    #2
    1. Advertising

  3. How do I set the "numberOfCheckboxes"?

    To test the code I replaced "numberOfCheckboxes" with 2 and that
    worked but then it choked on the if statement.

    I'm using win98 and IE 6.0

    Thanks...

    On Sun, 14 Dec 2003 17:27:26 +0100, Lasse Reichstein Nielsen
    <> wrote:

    >Ralph Freshour <> writes:
    >
    >> frm_chk_delete[0]
    >> frm_chk_delete[1]
    >> frm_chk_delete[2]
    >> frm_chk_delete[3]

    >...
    >> <INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
    >> $php_delete_counter;?>]">Delete

    >...
    >> I'd like to use JS before the submit and make sure at least one of the
    >> checkboxes is checked - how do I loop through these array name
    >> checkboxes in javascript to check their states before submitting?

    >
    >---
    ><script type="text/javascript">
    >function isOneSet(form) {
    > for (var i=0;i<numberOfCheckboxes;i++) {
    > if (form.elements["frm_chk_delete["+i+"]"].checked) {
    > return true;
    > }
    > }
    > return false;
    >}
    ></script>
    >---
    >and
    >---
    ><form ... onsubmit="if (!isOneSet(this)) {alert('SET ONE!');return false;}">
    >---
    >
    >/L
     
    Ralph Freshour, Dec 14, 2003
    #3
  4. Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
    GMT:

    > I have multiple checkbox's created with an array name because I
    > have many on the same web page - their names are like:
    >
    > frm_chk_delete[0]
    > frm_chk_delete[1]
    > frm_chk_delete[2]
    > frm_chk_delete[3]
    > etc.
    >
    > Here is my code line that creates each checkbox (the php
    > variable get incremented of course):
    >
    > <INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
    > $php_delete_counter;?>]">Delete
    >
    > When I click on the Delete button I want to call a JS routine to
    > check that at least one checknbox has been checked and marked
    > for deletion when the form submit is called my php file will
    > detect and delete the checkboxes accordingly.
    >
    > I'd like to use JS before the submit and make sure at least one
    > of the checkboxes is checked - how do I loop through these array
    > name checkboxes in javascript to check their states before
    > submitting?


    It would be easier if they all had the same name, but it can be done
    as it is.

    <FORM ... onsubmit="return checkBoxes(this)">

    function checkBoxes(form) {
    var numControls = form.elements.length;
    var checks = 0;

    // Check every control in the form
    for (var i = 0; i < numControls; ++i) {
    var element = form.elements;

    // If the name begins, frm_chk_delete[ ...
    if ('frm_chk_delete[' == element.name.substr(0, 15)) {
    // ...see if it's checked, and increment counter if it is
    if (element.checked) checks++;
    }
    }
    // If none of boxes have been checked, alert the user and cancel
    // the submission
    if (!checks) {
    window.alert('Please check at least one box before submitting');
    return false;
    }

    // If code reaches here, at least one box was checked, so allow
    // the form to submit
    return true;
    }

    If it's not guaranteed that only checkboxes will have names that
    start with 'frm_chk_delete[', you should also check to make sure that
    the element being inspected is a checkbox (use element.type ==
    'checkbox').

    Hope that helps,

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk")
     
    Michael Winter, Dec 14, 2003
    #4
  5. Ralph Freshour <> writes:

    > How do I set the "numberOfCheckboxes"?


    I don't know. It is the number of checkboxes you have created, so I'll
    assume you insert it with PHP somehow.

    > To test the code I replaced "numberOfCheckboxes" with 2 and that
    > worked but then it choked on the if statement.


    "choked"? Not a useful error report :)
    You'll have to be a little more informative, or we won't be able
    to help. What is the *exact* error message? Can we see the page?

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Dec 14, 2003
    #5
  6. Sorry - the error is in the lower left corner of my browser: "Error on
    page"

    That's all it says - the code below displays 1 and 2 and give the
    error msg - so it must be in the if statement:

    function atLeastOneChecked(form){
    alert("1");
    for (var i=0;i<2;i++) {
    alert("2");
    if (form.elements["frm_chk_delete["+i+"]"].checked) {
    alert("3");
    return true;
    }
    }
    alert("Please check at least one box!");
    return false;
    }




    On Sun, 14 Dec 2003 19:00:45 +0100, Lasse Reichstein Nielsen
    <> wrote:

    >Ralph Freshour <> writes:
    >
    >> How do I set the "numberOfCheckboxes"?

    >
    >I don't know. It is the number of checkboxes you have created, so I'll
    >assume you insert it with PHP somehow.
    >
    >> To test the code I replaced "numberOfCheckboxes" with 2 and that
    >> worked but then it choked on the if statement.

    >
    >"choked"? Not a useful error report :)
    >You'll have to be a little more informative, or we won't be able
    >to help. What is the *exact* error message? Can we see the page?
    >
    >/L
     
    Ralph Freshour, Dec 14, 2003
    #6
  7. this line is giving me "Error on page" msg in lower left corner of my
    browser:

    var numControls = form.elements.length;

    ???



    On Sun, 14 Dec 2003 17:55:13 GMT, Michael Winter
    <> wrote:

    >Ralph Freshour wrote on 14 Dec 2003 at Sun, 14 Dec 2003 16:00:11
    >GMT:
    >
    >> I have multiple checkbox's created with an array name because I
    >> have many on the same web page - their names are like:
    >>
    >> frm_chk_delete[0]
    >> frm_chk_delete[1]
    >> frm_chk_delete[2]
    >> frm_chk_delete[3]
    >> etc.
    >>
    >> Here is my code line that creates each checkbox (the php
    >> variable get incremented of course):
    >>
    >> <INPUT TYPE="checkbox" NAME="frm_chk_delete[<?php echo
    >> $php_delete_counter;?>]">Delete
    >>
    >> When I click on the Delete button I want to call a JS routine to
    >> check that at least one checknbox has been checked and marked
    >> for deletion when the form submit is called my php file will
    >> detect and delete the checkboxes accordingly.
    >>
    >> I'd like to use JS before the submit and make sure at least one
    >> of the checkboxes is checked - how do I loop through these array
    >> name checkboxes in javascript to check their states before
    >> submitting?

    >
    >It would be easier if they all had the same name, but it can be done
    >as it is.
    >
    ><FORM ... onsubmit="return checkBoxes(this)">
    >
    >function checkBoxes(form) {
    > var numControls = form.elements.length;
    > var checks = 0;
    >
    > // Check every control in the form
    > for (var i = 0; i < numControls; ++i) {
    > var element = form.elements;
    >
    > // If the name begins, frm_chk_delete[ ...
    > if ('frm_chk_delete[' == element.name.substr(0, 15)) {
    > // ...see if it's checked, and increment counter if it is
    > if (element.checked) checks++;
    > }
    > }
    > // If none of boxes have been checked, alert the user and cancel
    > // the submission
    > if (!checks) {
    > window.alert('Please check at least one box before submitting');
    > return false;
    > }
    >
    > // If code reaches here, at least one box was checked, so allow
    > // the form to submit
    > return true;
    >}
    >
    >If it's not guaranteed that only checkboxes will have names that
    >start with 'frm_chk_delete[', you should also check to make sure that
    >the element being inspected is a checkbox (use element.type ==
    >'checkbox').
    >
    >Hope that helps,
    >
    >Mike
     
    Ralph Freshour, Dec 14, 2003
    #7
  8. Ralph Freshour <> writes:

    > Sorry - the error is in the lower left corner of my browser: "Error on
    > page"


    Your browser is some version of Internet Explorer, correct?
    You can click on the error icon and get a better explanation (not good,
    just better).
    You can turn error messages on in:
    Tools > Internet Options > Advanced > Browsing :
    Display a notification about every scrip error.

    > That's all it says - the code below displays 1 and 2 and give the
    > error msg - so it must be in the if statement:


    > if (form.elements["frm_chk_delete["+i+"]"].checked) {


    If this fails, it is because something doesn't have the correct value.

    Either "form" is not pointing to the correct form, or it has no
    element called "frm_chk_delete[0]". Try adding alerts before the if:
    alert(form);
    alert(form.elements["frm_chk_delete[0]"]);

    Check that the form is written correctly.

    And please don't top post! At least trim your quotes.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Dec 14, 2003
    #8
  9. On Sun, 14 Dec 2003 21:46:52 +0100, Lasse Reichstein Nielsen
    <> wrote:

    >Ralph Freshour <> writes:
    >
    >> Sorry - the error is in the lower left corner of my browser: "Error on
    >> page"

    >
    >Your browser is some version of Internet Explorer, correct?
    >You can click on the error icon and get a better explanation (not good,
    >just better).
    >You can turn error messages on in:
    > Tools > Internet Options > Advanced > Browsing :
    > Display a notification about every scrip error.
    >
    >> That's all it says - the code below displays 1 and 2 and give the
    >> error msg - so it must be in the if statement:

    >
    >> if (form.elements["frm_chk_delete["+i+"]"].checked) {

    >
    >If this fails, it is because something doesn't have the correct value.
    >
    >Either "form" is not pointing to the correct form, or it has no
    >element called "frm_chk_delete[0]". Try adding alerts before the if:
    > alert(form);
    > alert(form.elements["frm_chk_delete[0]"]);
    >
    >Check that the form is written correctly.
    >
    >And please don't top post! At least trim your quotes.
    >
    >/L



    Thanks for the info about turning on the details of the err msg in IE
    - I saw the problem was in

    var numControls = form.elements.length;

    so in my calling statement I changed it from (this) to (this.form) and
    then it worked ok.

    Thanks for help everyone...
     
    Ralph Freshour, Dec 14, 2003
    #9
    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. =?Utf-8?B?YSBkZXZlbG9wZXI=?=

    datagrid and multiple checkboxes(hotmail model)

    =?Utf-8?B?YSBkZXZlbG9wZXI=?=, Nov 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    534
    Olivier Verdin
    Nov 6, 2004
  2. Replies:
    0
    Views:
    5,571
  3. joebob8000
    Replies:
    0
    Views:
    1,085
    joebob8000
    Dec 4, 2006
  4. Evil Monkey

    Checkboxes - help needed

    Evil Monkey, Mar 6, 2004, in forum: Javascript
    Replies:
    0
    Views:
    73
    Evil Monkey
    Mar 6, 2004
  5. JL
    Replies:
    7
    Views:
    201
    Thomas 'PointedEars' Lahn
    Mar 10, 2008
Loading...

Share This Page