Finding out how many checkboxes are in a table

stevewy

Usenet Archive
Joined
Aug 17, 2006
Messages
42
Reaction score
0
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.length)

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').elements.length)

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

Steve Wylie
 

Laser Lips

Usenet Archive
Joined
Apr 29, 2008
Messages
191
Reaction score
0
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.length)

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').elements.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);
 

stevewy

Usenet Archive
Joined
Aug 17, 2006
Messages
42
Reaction score
0
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
 

Gregor Kofler

Usenet Archive
Joined
Jul 28, 2007
Messages
1,065
Reaction score
0
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
 

stevewy

Usenet Archive
Joined
Aug 17, 2006
Messages
42
Reaction score
0
Okay, thanks for the info. And back to the drawing board I go...

Steve
 

stevewy

Usenet Archive
Joined
Aug 17, 2006
Messages
42
Reaction score
0
What's with the "???" Laserlips? You have a query with what I said?

Steve
 

stevewy

Usenet Archive
Joined
Aug 17, 2006
Messages
42
Reaction score
0
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,243
Members
248,133
Latest member
DujonT

Latest Threads

Top