469,323 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

fun with checkboxes

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[i].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

Apr 19 '06 #1
3 1265
pa******@gmail.com 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[i].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;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 19 '06 #2
> 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
Apr 19 '06 #3
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[i].checked)
{
players++;
}

alert('Players = ' + players);
}

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

}

pa******@gmail.com wrote:
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[i].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


Apr 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ralph Freshour | last post: by
5 posts views Thread by @(none) | last post: by
6 posts views Thread by terence.parker | last post: by
8 posts views Thread by PeteOlcott | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.