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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How would one list a form's elements?

    Hi...

    Within a document, there can be any number of forms.
    Is there any reference (code example) that would guide one on best practices on how to loop through existing document forms, exposing it's elements and their values? ...and possible the element's type (text, textarea, etc)?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    var allForms = document.forms;//forms' collection
    var allEle =[]
    for (var i=0;i<allForms.length;i++){
    allEle[i]=[]
    allEle[i][allEle[i].length]=allForms[i].elements;//elements' collection
    }

    the elements collection returns a 2nd array,

    allEle[i][j]

    where i=form's index, j=element's index in that form
    You may now circle through the elements, using as criteria the type.

    Well yes, you may build a simple array, if you don't need to know which form's the elements belong. In this case, a DOM solution is easiest

    var allInputs=document.getElementsByTagName('input')//inputs collection
    var allTextareas = document.getElementsByTagName('textarea')//textareas collection

    now you can circle upon type, as above...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    thanks for the timely assist!

    enjoy the day!

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    or, much simplier, just sort them by type

    var allTags = document.getElementsByTagName('*');//all tags collection
    var allChecks =[];//the checkboxes collection
    var allTextBoxes =[];//the textboxes collection
    ....
    for(var i=0;i<allTags.length;i++){
    if(allTags[i]getAttribute('type')=='checkbox'){
    allChecks[allChecks.length]=allTags[i]
    }
    else if(allTags[i]getAttribute('type')=='text'){
    allTextBoxes[allTextBoxes.length]=allTags[i];
    }
    ...
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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