SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member PMenard's Avatar
    Join Date
    Jul 2003
    Location
    Mars
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form array checkbox accesing

    First I'm sure the answer to ths JS question is buried somewhere in the many item of this forum. Just could not find it.

    So I'm working on changes to a form developed by someone else. In the form the previous developer wrote the following:

    <input type="checkbox" name="method[]" value="payroll_onetime">Payroll
    <input type="checkbox" name="method[]" value="payroll_onetime">One-time
    <input type="checkbox" name="method[]" value="paper">Paper Check
    <input type="checkbox" name="method[]" "onlinebill">Online Bill

    Note there are other form and HTML I removed.

    So my problem is this. I'm trying to access the method checkbox array from a JS function. So I write:

    function setPayrollStatus()
    {
    alert(document.profile.method.length);
    }

    This does give me that value of 4. Since there are 4 checkboxes for method I'm thinking this is correct. So next I add:

    for (var idx=0; idx < document.profile.method.length; idx++)
    {
    alert(document.profile.method[idx].checked);
    }

    Now I get only 'undefined'.


    What I want to accomplish is to check the first item. I thought I would be able to write something like:
    document.profile. method[0].checked = true;


    If I change the form name tags from 'method[]' to 'method' it works. Problem is he also wrote the php back end processing for this form. And in PHP this syntax yeilds an array for the method vriable.

    What am I missing?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you cant chage method[] suggest you identify the check box by another method

    for example

    PHP Code:
    <body>
    <
    form name="profile" id="profile" >
    <
    input type="checkbox" name="method[]" value="payroll_onetime">A Another
    <input type="checkbox" title="grp1" name="method[]" value="payroll_onetime">Payroll
    <input type="checkbox" title="grp1" name="method[]" value="payroll_onetime">One-time
    <input type="checkbox" title="grp1" name="method[]" value="paper">Paper Check
    <input type="checkbox" title="grp1" name="method[]" "onlinebill">Online Bill
    </form>
    Note there are other form and HTML I removed.

    So my problem is thisI'm trying to access the method checkbox array from a JS function. So I write:

    <script language="JavaScript" type="text/javascript">
    <!--

    function setPayrollStatus(){
     grp1Ary=new Array();
     myips=document.getElementById('
    profile').getElementsByTagName('INPUT');
     for (i=0;i<myips.length;i++){
      if (myips[i].getAttribute('
    title')){
       if (myips[i].title=='
    grp1'){
        grp1Ary[grp1Ary.length]=myips[i];
       }
      }
     }
     grp1Ary[0].checked=true;
    }

    setPayrollStatus()
    //-->
    </script> 


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
  •