Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Finding out how many checkboxes are in a table

Reply
Thread Tools

Finding out how many checkboxes are in a table

 
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      10-07-2008
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
 
Reply With Quote
 
 
 
 
Laser Lips
Guest
Posts: n/a
 
      10-07-2008
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);

 
Reply With Quote
 
 
 
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      10-07-2008
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
 
Reply With Quote
 
Gregor Kofler
Guest
Posts: n/a
 
      10-07-2008
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
 
Reply With Quote
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      10-07-2008
Okay, thanks for the info. And back to the drawing board I go...

Steve
 
Reply With Quote
 
Laser Lips
Guest
Posts: n/a
 
      10-07-2008
On Oct 7, 1:35*pm, stev...@hotmail.com wrote:
> Okay, thanks for the info. *And back to the drawing board I go...
>
> Steve


???
 
Reply With Quote
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      10-08-2008
What's with the "???" Laserlips? You have a query with what I said?

Steve
 
Reply With Quote
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      10-08-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
finding out if a linq table has 0 rows Andy B ASP .Net 2 11-20-2008 07:19 AM
Finding checkboxesl between 2 checkboxes Mark Javascript 1 09-19-2006 03:34 PM
Finding out if a Mysql database contains a certain table ngw Ruby 2 02-19-2006 06:45 PM
modeling an object many to many relationship with a transactional bridge table Jim Thomason Perl Misc 3 10-02-2004 05:06 PM



Advertisments