• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CheckBoxes with same name

 
Anoop Krishnan
Ranch Hand
Posts: 163
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
formName.elements[index];
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
Dave
 
Heather Bosley
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you have select boxes with the same name??
 
Mike Yu
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic