473,385 Members | 1,901 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,385 software developers and data experts.

Finding out how many checkboxes are in a table

I'm looking to manipulate/check the state of various checkboxes and
radios in a form. Because I don't want to access all of the
checkboxes in the form, only a specific section, I am trying to narrow
down the list of elements to check. I thought I could place those
elements within a named DIV or a table, and then reference that DIV or
table by its ID. But it's not working.

Say for instance I wanted Javascript to issue an alert (just for
testing purposes), giving me the number of elements in a Table. If
the Table's ID was "fred", why does the instruction below not work?

alert(document.getElementById('fred').elements.len gth)

It comes up with an error saying it's "Null or not an object". Any
ideas? Is this just not possible, or am I doing it wrong?

If I use the name of the form (which is called "form" as well), to do:

alert(document.form.getElementById('fred').element s.length)

.... it says the Object doesn't support this property or method.
Steve Wylie
Oct 7 '08 #1
7 3374
On Oct 7, 11:26*am, stev...@hotmail.com wrote:
I'm looking to manipulate/check the state of various checkboxes and
radios in a form. *Because I don't want to access all of the
checkboxes in the form, only a specific section, I am trying to narrow
down the list of elements to check. *I thought I could place those
elements within a named DIV or a table, and then reference that DIV or
table by its ID. *But it's not working.

Say for instance I wanted Javascript to issue an alert (just for
testing purposes), giving me the number of elements in a Table. *If
the Table's ID was "fred", why does the instruction below not work?

alert(document.getElementById('fred').elements.len gth)

It comes up with an error saying it's "Null or not an object". *Any
ideas? *Is this just not possible, or am I doing it wrong?

If I use the name of the form (which is called "form" as well), to do:

alert(document.form.getElementById('fred').element s.length)

... it says the Object doesn't support this property or method.

Steve Wylie

Because document.getElementById('fred').elements is not an valid
object, where did you get that from?

you can't just make things up as you go along

Use ...

tab = document.getElementById("fred");
inputs=tab.getElementsByTagName("input");
numOfCB=0;
for(x=0;x<inputs.length;x++)
{
if(inputs[x].type=="checkbox")numOfCB++;
}
alert(numOfCB);

Oct 7 '08 #2
I based my (failed) example on changing a working example I got from
the internet, which was used for something else. Clearly it doesn't
translate to work in this case!

You provided an answer to the example I gave about finding how many
checkboxes there are in the Table "Fred", but unfortunately this is
not actually what I am trying to achieve, I just gave that as an
example to try to understand how I am meant to properly deal with
elements in a certain part of a form. I'll try and explain it better:

If I wanted to deal with all the elements in a form (called "form"), I
could use "alert(form.elements.length)", which would tell me how many
elements there were in the entire form. So if I wanted to find out if
a certain checkbox was ticked, and I knew the element number of that
checkbox, I could address it directly with form.elements[47]. At
least I think this is how it works.

What I was trying to do, if it was possible in Javascript, was to
access just the form elements that were present in a named table. I
am imagining here that the first form element in the table would be
element [0]. Or is what I want totally not possible in Javascript,
and the form elements have to be referenced across the entire form?

I mean, if I had a Table with an ID of "Fred", can I reference certain
properties of the table by using "fred.property"?

It is apparent I have little understanding of the way this is supposed
to work, so any advice you can give me would be appreciated. I did
try Googling for a solution, but could not find anything that told me
what I needed to know...

Steve
Oct 7 '08 #3
st*****@hotmail.com meinte:

[snip]

Only forms have an elements collection.
It is apparent I have little understanding of the way this is supposed
to work, so any advice you can give me would be appreciated.


Once more:

var table = document.getElementById("myTable");
var inputs = table.getElementsByTagName("input");
var selects = table.getElementsByTagName("select");
var textareas = table.getElementsByTagName("textarea");
....

Either combine those collections or iterate through each of them
seperately. Whether you have a form somewhere is in this case completely
irrelevant.

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Oct 7 '08 #4
Okay, thanks for the info. And back to the drawing board I go...

Steve
Oct 7 '08 #5
On Oct 7, 1:35*pm, stev...@hotmail.com wrote:
Okay, thanks for the info. *And back to the drawing board I go...

Steve
???
Oct 7 '08 #6
What's with the "???" Laserlips? You have a query with what I said?

Steve
Oct 8 '08 #7
Ah, sorry. Looked again at both your responses and figured out that
you had both answered my question. Once I'd worked out what you were
getting at. So if I wanted to isolate just the checkboxes in an area
of my document called "q2", and check the last one of them, following
your instructions above I could do:

function tickq2(){
tab = document.getElementById("q2");
cbs=tab.getElementsByTagName("input");
cbs[cbs.length-1].checked=1
}

Actually, it wouldn't isolate just the checkboxes in that section of
the document, but all of the input tags whatever they were; but I
could certainly work around that.

So, thank you for the assistance, Gregor and Laser Lips.

Oct 8 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Deborah V. Gardner | last post by:
I would like to use "Yes" and "No" checkboxes on a subform. The problem is that when I click the Yes checkbox on the subform, all of the checkboxes are checked. Currently, I have a field...
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...
1
by: Mark | last post by:
Hi - I'm using asp.net to draw a grid (dates and people) which is populated by checkboxes - each check box represents a day and a person (so for example, I may have 2 weeks showing across the top,...
9
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if...
2
by: Geovanni Cesar | last post by:
I have a table with 12 checkboxes. I have the following function which when I check the last checkbox in the table, the first 10 checkboxes in the table are unchecked. - function...
2
by: avanti | last post by:
Hi, I add rows to my table dynamically, with a checkbox in each row in the first column. The checkbox in the header row acts as a 'Select All' checkbox. Whenever it is checked, checkboxes in all...
0
by: krokador | last post by:
This is doing my head in... We're starting to migrate our report-printing and such (forms included) to pdf - using xml files and nFOP with asp.net. In this case I have to print out a form which...
1
by: TechnoAtif | last post by:
Hi to all. I have got a form containing of checkboxes along with other items. I have simply no clue as to (i) how to make entry for those checkbox data into the mysql table . I mean : what query...
14
by: zufie | last post by:
I have to create a QA report regarding callers calling into a phone hotline. The report consists of many checkboxes such as: Did the IBCCP agency contact you? Yes/NO How many days passed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.