www.webdeveloper.com
Results 1 to 9 of 9

Thread: variables problem

  1. #1
    Join Date
    Jan 2005
    Location
    Sitges, Barcelona
    Posts
    9

    variable problem

    i would like to do this, but no works:


    function putOn()
    {
    for (i = 0; i <document.f.elements.length; i++)
    {
    document.f.c"i".checked = true; ( checkbox)
    document.f.st"i".value ='5'; (combo box)
    }
    }



    I have multiple checkboxes, and combo boxes with this names:
    document.f.c1, document.f.st1
    document.f.c2, document.f.st2
    """


    and I woul like to reference all documents
    I tried to do this:

    document.f.c+i+.checked = true;
    document.f.st+i+.value ='5';

    but no works
    Last edited by becknetstat; 01-04-2005 at 07:00 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Objects in JavaScript can also be addressed as associative arrays:

    document.f.c['i'].checked = true
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    Jan 2005
    Location
    Sitges, Barcelona
    Posts
    9
    thanks Charles


    I tried to to this, but no works

    function putOn()
    {
    for (i = 0; i <document.f.elements.length; i++)
    {
    document.f.c['i'].checked = true;
    document.f.st['i'].value = '5';
    }
    }


    for example, in Php I can do:
    PHP Code:
    for ($i=1$i<$max$i++)
    {
    document.f.st".$i.".value'5';
    document.f.c".$i.".checked 1;

    and create a loop for for do this:

    document.f.st1.value='5'
    document.f.c1.checked= 1

    document.f.st2.value='5'
    document.f.c2.checked= 1

    document.f.st3.value='5'
    document.f.c3.checked= 1

    document.f.st4.value='5'
    document.f.c4.checked= 1

    but I donīt know hom do something similar in javascript

  4. #4
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    Try:

    function putOn()
    {
    for (i = 0; i <document.f.elements.length; i++)
    {
    document.f.c[i].checked = true;
    document.f.st[i].value = '5';
    }
    }

  5. #5
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    <HTML>
    <Head>
    <Script Language=JavaScript>

    function putOn(isForm){

    nElements = isForm.length;
    for (i=0; i<nElements; i++)
    {
    if (isForm[i].type == 'checkbox'){isForm[i].checked = true}
    if (isForm[i].type == 'text'){isForm[i].value = '5'}
    }
    }

    </Script>
    </Head>
    <Body>
    <Form name='f'>
    C1: <input type=checkbox name='c1'>
    C2: <input type=checkbox name='c2'>
    C3: <input type=checkbox name='c3'>
    <br>
    ST1: <input type=text name='st1' size=5>
    ST2: <input type=text name='st2' size=5>
    ST3: <input type=text name='st3' size=5>
    <br>
    <input type=button value="Put On" onclick="putOn(this.form)">
    </Form>
    </Body>
    </HTML>

  6. #6
    Join Date
    Jan 2005
    Location
    Sitges, Barcelona
    Posts
    9
    thanks for your answers

    I tried to do

    i = 1;
    document.f.c[i].checked = true;

    no works
    error: object 'document.f.c' is null or not an object

    but if I do this:
    document.f.c1.checked = true;

    WORKSSS !!!!!, checkbox "c1" its selected, but I canīt do a bucle for select all the checkboxes "c[i]" example.(c1, c2, c3, c4, c5,....)

    WHY????

  7. #7
    Join Date
    Dec 2004
    Posts
    364
    Just use something like this in your loop:

    document.f['c'+i].checked=true;

    PJ

  8. #8
    Join Date
    Jan 2005
    Location
    Sitges, Barcelona
    Posts
    9
    I tried your solution:
    document.f['c'+i].checked=true;

    and.....

    WORKS



    ThanKīs

  9. #9
    Join Date
    Dec 2004
    Posts
    364
    You're welcome!
    PJ

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles