Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through checkboxes in a form collection

Status
Not open for further replies.

magmo

Programmer
May 26, 2004
291
0
0
SE
Hi


I have a form that has x number of values in itself. It is filled with data from a database, each row from the database make a new position in the form. And each position has a checkbox to it. The checkboxes has a javascript assigned to it, so if all checkboxes that has the same name is checked when the user ccheck them.

So a typical form would look like this....

Code:
<form>
<input name="0.Check" type="checkbox" value="Add" onclick="checkAll(this)">
<input type="hidden" name="0.Book" value="Book no 1A">

<input name="0.Check" type="checkbox" value="Add" onclick="checkAll(this)">
<input type="hidden" name="0.Book" value="Book no 1B">

<input name="1.Check" type="checkbox" value="Add" onclick="checkAll(this)">
<input type="hidden" name="1.Book" value="Book no 2">

<input name="12.Check" type="checkbox" value="Add" onclick="checkAll(this)">
<input type="hidden" name="2.Book" value="Book no 3">


<input type="hidden" name="Count" value="4">
<input type="submit" name="Submit" value="Submit">
</form>

I then use this code to loop through them...

Code:
iCount = Request.Form("Count")
For iLoop = 0 to iCount
Response.Write Request(iLoop & ".Book") & ("<br>")
  Response.write("<br><br>") & vbCrlf
Next


But then I get this result....

Book no 1A, Book no 1B
Book no 2
Book no 3


When I really would like this result...

Book no 1A
Book no 1B
Book no 2
Book no 3

What changes do I need to do get this to work.


Regards



 


The Form results are grouped by the 'name' attribute in the input - that's why you are getting 'Book no 1A, Book no 1B' in the same var (0.Book)

Also your 'count' will equal 4, though there is no 3 or 4 as the prefix to the '.Book' part of the name of the posted input (only 0.Book, 1.Book and 2.Book)

Please post your javascript function so that we can see what this does in relation to these checkboxes

If you want them to be separate in the form result you will need to give them different names.


A smile is worth a thousand kind words. So smile, it's easy! :)
 
Hi

Here it is...


Code:
<script>
function checkAll(c){
  var chks = document.getElementsByName(c.name);
  for(var i=0;i<chks.length;i++){
    chks[i].checked = c.checked;
  }
}
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top