Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    loop through form elements wiith an id

    Hi all.

    I have a form named "theForm". How can I loop through this form elements that have an "id" ATTRIBUTE to them and then populate my ids array?


    var ids = new Array;

    for(i=0; i<document.theForm.elements.length; i++)
    {

    IF ELEMENT HAS AN ID ATTRIBUTE POPULATE ids array

    }
    Last edited by nikko50; 10-13-2011 at 01:34 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,995
    Thanks
    79
    Thanked 4,433 Times in 4,398 Posts
    Code:
    var ids = [ ];
    
    var form = document.theForm;
    
    for( i=0; i  < form.elements.length; i++ )
    {
        var elem = form.elements[i];
        if ( elem.id != null && elem.id != "" )
        {
            ids.push( elem.id );
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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