File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes CheckBoxes with same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "CheckBoxes with same name" Watch "CheckBoxes with same name" New topic

CheckBoxes with same name

Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
I have many checkboxes in my html page with same name
How can i access the values or count of all selected checkbox from the java script function

I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Since they all have the same name you need to use the elements array to acces them. The elements array holds all of the objects in the form (checkboxes, radiobuttons, text fields, etc).
access it like this:
to see if the item is a checkbox you could test it this way:
if (formName.elements[index].type == "checkbox")
and then just add a test to see if it selected or not:
if (formName.elements[index].type == "checkbox" && formName.elements[index].checked)
if the above line is true then the element is checkbox that is checked.
Loop through all of the elements in the elements array with:
for (var i=0; i < formName.elements.length; i++)
hope that helps

Heather Bosley

Joined: Oct 23, 2002
Posts: 1
Ok, I can do checkboxes, but what if you have multiple SELECT BOXES with the same name. How do you reference each one's selected value?
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Why would you have select boxes with the same name??
Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
I used multiple select boxes with same name. It is very useful in some cases. For example, if the number of select boxes is unknown at development time, and we need to retrieve their values when the page is submitted.

Thanks,<br />Mike
I agree. Here's the link:
subject: CheckBoxes with same name
It's not a secret anymore!