...

View Full Version : How would one list a form's elements?



larsonr
10-18-2005, 11:32 AM
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)?

Kor
10-18-2005, 12:58 PM
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...

larsonr
10-18-2005, 04:54 PM
thanks for the timely assist!

enjoy the day! :) :)

Kor
10-18-2005, 05:20 PM
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];
}
...
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum