Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding out the number of elements in a form

Status
Not open for further replies.

ninerloyal

Programmer
Jan 25, 2007
2
US
Does anyone know if there is a good way to find out the number of elements in a form?

For example, if I had the following form:

<FORM NAME=TEMP>
<INPUT>
<INPUT>
<INPUT>
<INPUT>
</FORM>

is there a way to set up a variable that would be

var numElements = 4;

for this simple form?
 
Code:
var myForm = document.getElementByID("myFormID");
var allInputs = myForm.getElementsByTagName("input");
var numElements = allInputs.length;

Of course that only counts input elements on the form and not textarea, button or img controls... but the same principle can be applied to them and the totals added together if need be.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
This contains the needed info. How to use the data depends on the need.
[tt]
function showit() {
var celem=document.formname.elements;
var s="# of elements in the form: "+celem.length+"\n";
for (var i=0;i<celem.length;i++) {
s+=celem.tagName+"\t"+celem.name+"\t"+celem.type+"\n";
}
alert(s);
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top