By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,287 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,287 IT Pros & Developers. It's quick & easy.

Finding out how many checkboxes are in a table

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Okay, thanks for the info. And back to the drawing board I go...

Steve
Oct 7 '08 #5

P: n/a
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

P: n/a
What's with the "???" Laserlips? You have a query with what I said?

Steve
Oct 8 '08 #7

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.