www.webdeveloper.com
Results 1 to 5 of 5

Thread: Moving through controls

  1. #1
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    13

    Moving through controls

    Hi all,
    I can't seem to get this to work.

    I have an unknown number of textboxes on my form. They will be called person(i) so if I have 3 textboxes I will have person1, person2, and person3. I want to be able to create a for loop that will loop through each one of these controls...

    Something like

    For i = 1 to 3
    {
    document.myform.personi.value
    }

    How do I increment the control name correctly?

    Cheers,
    Mike

  2. #2
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    Try something like this:

    PHP Code:
    for (i=0i<NumElemi++){
        
    BoxName='Person'+i;
        
    BoxValue=document.myform.eval(BoxName).value;


  3. #3
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    13
    Cool deal,
    Thanks a lot. This will save me a lot of time and make my code look a little bit nicer.

  4. #4
    Join Date
    Jul 2003
    Location
    Boston Area, Massachusetts
    Posts
    3,489
    You just about never need to use eval; the only time I've ever seen to prove useful is to avoid scoping issues, but that isn't the case in this instance. You need to recall that the form object has an elements array in which it's child elements can be accessed though. It could be as simple as this:

    for(i=0 i<theForm.elements.length; i++) theForm.elements['element' + i ].value = 'This is element ' + i + '.';

    This is where theForm is a reference to the form object you want to access. The main reason you would want to avoid using eval is becuase it uses more resources than just about any other JavaScript method. It just doesn't make sense to waste precious resources when you have other (less resource heavy) methods available to you. The more calls you make to eval, the longer it will take for your script to execute, and that really isn't what you want.

    Good luck.
    Last edited by fredmv; 11-17-2003 at 02:46 PM.

  5. #5
    Join Date
    Oct 2003
    Location
    New Zealand
    Posts
    13
    OK, that makes sense... But if I am using asp to create these controls how can I tell what the elements # is?

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