473,224 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 1392
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
8
by: DylanM | last post by:
I have some checkboxes that are generated from the results of a database search. At the moment, the checkboxes are part of a table making up a form. Users are going through the form, clicking the...
8
by: Ralph Freshour | last post by:
I have multiple checkbox's created with an array name because I have many on the same web page - their names are like: frm_chk_delete frm_chk_delete frm_chk_delete frm_chk_delete etc. Here...
5
by: @(none) | last post by:
I have a page which is a set of CheckBoxes generated daily and thus the number of Checkboxes changes each day. What I want to do is allow the user to select one or more checkboxes and the push a...
6
by: terence.parker | last post by:
I currently have the following JS in my header: function checkall(thestate) { var checkboxes=eval("document.forms.EssayList.file_id") for (i=0;i<checkboxes.length;i++)...
2
by: john | last post by:
I posted this question to comp.lang.javascript but didn't get a response, so I'll try here. I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
5
by: masterej | last post by:
Developers, Is there any way to disable all checkboxes on a form? I have a form with 160 checkboxes and I want to be able to disable all of them. Is there a way I can do something like this: ...
8
by: PeteOlcott | last post by:
http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/grid/tests/test_change_structure.html I would like to completely understand how GUI controls such as this one are constructed. In the...
7
by: viki1967 | last post by:
I need one function javascript that: 1) when I enter in this htm page I see enabled only checkbox of categories A, M and T; checkboxes of microcategories all disabled; 2-a) If I select the...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.