Multiple text boxes with the same name

E

Ellie100

Hi all

I'm a first-time poster and would appreciate some help with a problem
I'm having. I have an HTML form (UPD_BUDG) which includes 10
checkboxes, all with the same name (Apply_Phasing_CB) but different
values. On its own (i.e. before I add any JS) the form works fine.

I need to add an onSubmit function which counts how many of the boxes
are checked. Here is the script I have used:

function check_form()
{
var check_count = 0;
for(i = 0; i < 10; i++)
{
if (document.forms.UPD_BUDG.Apply_Phasing_CB.checked)
{
check_count++;
}
}
return check_count;
}


When I run this I get the error message
'document.forms.UPD_BUDG.Apply_Phasing_CB[...].checked is not an
object'. I am running this on IE 5.

Can anyone advise me on what I'm doing wrong?

Thanks

Ellie
 
J

Janwillem Borleffs

Ellie100 said:
I need to add an onSubmit function which counts how many of the boxes
are checked. Here is the script I have used:
....

When I run this I get the error message
'document.forms.UPD_BUDG.Apply_Phasing_CB[...].checked is not an
object'. I am running this on IE 5.

Can anyone advise me on what I'm doing wrong?

Must be an error in your HTML (didn't close the form tag perhaps?), because
the following works for me:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function check_form() {
var obj = document.UPD_BUDG.Apply_Phasing_CB;
var check_count = 0;
for (var i = 0; i < obj.length; i++) {
if (obj.checked) check_count++;
}
return check_count;
}
</script>
</head>

<body>
<form name="UPD_BUDG">
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="button" onclick="alert(check_form())" value="Check">
</form>
</body>
</html>


JW
 
E

Ellie100

Janwillem Borleffs said:
Must be an error in your HTML (didn't close the form tag perhaps?), because
the following works for me:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
function check_form() {
var obj = document.UPD_BUDG.Apply_Phasing_CB;
var check_count = 0;
for (var i = 0; i < obj.length; i++) {
if (obj.checked) check_count++;
}
return check_count;
}
</script>
</head>

<body>
<form name="UPD_BUDG">
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="checkbox" name="Apply_Phasing_CB"><br />
<input type="button" onclick="alert(check_form())" value="Check">
</form>
</body>
</html>


JW


Thanks JW. Referring to the object as a variable, as you did, rather
than directly, seems to have done the trick. If anyone can explain
why this should be, I'd be interested!

Thanks again

Ellie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top