fun with checkboxes

P

payne747

Hi everyone,

Can anyone help me with the following code, for some reason I can't
understand if I'm being thick of if javascript is so backwards!

HTML defines some checkboxes such as:

<form name="players_form">
<input type="checkbox" name="check1" value="blabla"
onclick="UpdatePlayers()" />
<input type="checkbox" name="check2" value="blabla"
onclick="UpdatePlayers()" />
.... (10 checkboxes in total)
</form>

The UpdatePlayers() function looks like this:

function UpdatePlayers()
{
for(i=0; i<10; i++) // always will be 10 checkboxes so run 10
times.
{
if (document.players_form.check.checked == true)
{
players++;
}
else {
players--;
}
}

document.all.output.innerHTML = players;
}

This bit of code is supposed to return the amount of boxes checked via
the 'players' variable. However the error thrown back is:

document.players_form.check - null or unknown object

For some reason, the variable 'i' is not being appended to the check
name. Does anyone know of a better way to do this? Basically the aim is
to just dynamicaly count the amount of checked boxes on a screen.

Thanks!

Payne747
 
R

Randy Webb

(e-mail address removed) said the following on 4/19/2006 7:25 AM:
Hi everyone,

Can anyone help me with the following code, for some reason I can't
understand if I'm being thick of if javascript is so backwards!

You are being thick.
HTML defines some checkboxes such as:

<form name="players_form">
<input type="checkbox" name="check1" value="blabla"
onclick="UpdatePlayers()" />
<input type="checkbox" name="check2" value="blabla"
onclick="UpdatePlayers()" />
.... (10 checkboxes in total)
</form>

checkboxes can share a common name.

<input type="checkbox" name="myCheckboxes" value="blabla"
onclick="UpdatePlayers()" />

The UpdatePlayers() function looks like this:

function UpdatePlayers()
{
for(i=0; i<10; i++) // always will be 10 checkboxes so run 10
times.
{
if (document.players_form.check.checked == true)


If you are intent on naming them the way you named them:

if (document.players_form.elements['check'+i].checked)

The == true is superfluous and not needed. The .checked property is a
boolean.
{
players++;
}
else {
players--;
}
}

document.all.output.innerHTML = players;

document.getElementById('output').innerHTML = players;
 
C

Chris Diver

For some reason, the variable 'i' is not being appended to the check
name. Does anyone know of a better way to do this? Basically the aim is
to just dynamicaly count the amount of checked boxes on a screen.

Well it wont, your code is trying to access an element in an array
that doesn't exist. Try changing each check box to :

<input type="checkbox" name="check" etc.. />

This should create an array of checkboxes and your
other code *should* then work.

HTH

Chris
 
P

payne747

ah ha! figured it out finally thanks to you guys and a bit of pizza,
for reference, heres the final code to count the amount of checked
boxes:

function UpdatePlayers() {

for(i=0; i<10; i++)
{
if(document.players_form.check.checked)
{
players++;
}

alert('Players = ' + players);
}

document.all.output.innerHTML = players;
players = 0;

}
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top