View Full Version : Length of HTML form object

07-12-2007, 03:58 AM

I found one thing.
Assume that I have checkedbox object in my form:

1.) If I have more than one checkbox object with the same name (i.e mycheckbox") I have no problem to get the length of the checkbox in javascript.

2.) I have problem in getting the length of the checkbox object if the form contain only 1 checkbox object, wuth error "undefine", why???

Pls advise....

07-12-2007, 04:57 AM
Since you have no script to it is not clear how you are getting the length.
In general though, it is an input object with a type of checkbox.
If you do
<script type="text/javascript">
window.onload = function () { alert(document.getElementsByTagName('input').length); }
// -->
you will get 1 if there is only one input.
You would have to check the input type to see if it is a checkbox.

07-12-2007, 06:20 PM
If you have only one checkbox, there's no length property because it doesn't form an array.

var chks = document.forms[0].mycheckbox;
if (chks.length){ //multiple checkbox
for (var i=0; i<chks.length; i++){
else { //single checkbox

07-19-2007, 10:24 AM
Yup, this is what I want!!