• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

CheckBoxes with same name

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you have select boxes with the same name??
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic