473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_f orm">
<input type="checkbox" name="check1" value="blabla"
onclick="Update Players()" />
<input type="checkbox" name="check2" value="blabla"
onclick="Update Players()" />
.... (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.playe rs_form.check[i].checked == true)
{
players++;
}
else {
players--;
}
}

document.all.ou tput.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.player s_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 1403
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_f orm">
<input type="checkbox" name="check1" value="blabla"
onclick="Update Players()" />
<input type="checkbox" name="check2" value="blabla"
onclick="Update Players()" />
.... (10 checkboxes in total)
</form>
checkboxes can share a common name.

<input type="checkbox" name="myCheckbo xes" value="blabla"
onclick="Update Players()" />

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.playe rs_form.check[i].checked == true)
If you are intent on naming them the way you named them:

if (document.playe rs_form.element s['check'+i].checked)

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

document.all.ou tput.innerHTML = players;


document.getEle mentById('outpu t').innerHTML = players;

--
Randy
comp.lang.javas cript 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.pla yers_form.check[i].checked)
{
players++;
}

alert('Players = ' + players);
}

document.all.ou tput.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_f orm">
<input type="checkbox" name="check1" value="blabla"
onclick="Update Players()" />
<input type="checkbox" name="check2" value="blabla"
onclick="Update Players()" />
... (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.playe rs_form.check[i].checked == true)
{
players++;
}
else {
players--;
}
}

document.all.ou tput.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.player s_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
3955
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 checkboxes are picked up using $_POST and put into session variables. On page two there is another form which is simply a condensed version of the...
8
2821
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 boxes and saving to the database at the end with the 'Submit' command button. Is it possible to save the changes as the checkboxes are clicked? I...
8
2826
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 is my code line that creates each checkbox (the php variable get
5
5881
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 "Done" button and then have a script which uses a "for" loop to check the status of each box. The code I use for this is Sample checkbox HTML ...
6
2842
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++) checkboxes.checked=thestate } This works fine - but the problem is that it doesn't work with my
2
3183
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 user clicks on a certain button on the page, which is not in the grid, I want to be able to traverse through all the checkboxes in that column and see...
10
5190
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this...
5
11261
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: for (int i = 0; i < 160; i++) { string checkbox = "checkBox" + i.toString(); (Cast)checkbox.enabled = false;
8
6026
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 ideal case I would like to see all of the source code for a complete working example of a ListBox of CheckBoxes.
7
2287
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 checkbox of macrocategory A, M and T checkboxes all disabled; 2-b) If I select the checkbox of macrocategory M, A and T checkboxes all disabled; 2-c)...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.