Click to See Complete Forum and Search --> : number of text boxes in document?


chadorbaf
06-16-2003, 05:41 PM
Hi,
How could i find the number of text boxes (input boxes with
type="text") in a document?

Charles
06-16-2003, 05:53 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
var text = 0;
onload = function () {
for (i=0; i<document.forms.length; i++) {for (j=0; j<document.forms[i].elements.length; j++) {if (document.forms[i].elements[j].type == 'text') text++}};
alert (text);
}
// -->
</script>
<form action="">
<div>
<input type="text">
<input type="text">
<input type="text">
</div>
</form>

brendandonhue
06-16-2003, 05:54 PM
alert(document.getElementsByTagName("input").length)
Will get you all the <input> tags.
Maybe you can use some sort of loop to check if the type of each input tag is text.

Jona
06-16-2003, 05:56 PM
<script type="text/javascript">
<!--
var i, z;
function findBoxes(f){
for(i=0;i<f.elements.length;i++){
if(f.elements[i].type=="text"){z+=1;}
}
alert("There are "+z+ " textboxes in the form.");
}
//-->
</script></head>
<body>
<form name="myForm"><div>
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="text" name="t3"><br>
<input type="checkbox" name="c1">Checkbox 1<br>
<input type="checkbox" name="c2">Checkbox 2<br>
<input type="button" name="butn1" value="How many text boxes are in this form?" onClick="findBoxes(this.form);">
</div></form>
</body></html>


Jona

chadorbaf
06-16-2003, 06:13 PM
Originally posted by brendandonhue
alert(document.getElementsByTagName("input").length)
Will get you all the <input> tags.
Maybe you can use some sort of loop to check if the type of each input tag is text.

This one works good enough for me. The only thing is: i have some hidden input tags on the form, Any way to find only the number of visible input tags without going thro all input tags?

Thanks.

brendandonhue
06-16-2003, 06:18 PM
Nope. Use jona or charle's method. Its only a couple lines longer.

chadorbaf
06-16-2003, 06:21 PM
orright, thanks all you guys.