# Handling multiple Check boxes in a XHTML Strict way

**URL:** <https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697>\
**Category:** Rails\
**Created:** [January 31, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697 "2008-01-31T14:35:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sazwqa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@sazwqa](https://www.ruby-forum.com/u/sazwqa)\
**Post date:** [January 31, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697/1 "2008-01-31T14:35:39Z")

</div>

Hi,

The default way we handle an array of checkboxes is like this:

\<% items.each do |item| %\>  
\<%= check\_box\_tag ‘item[]’, “#{item.id}” %\>  
\<% end -%\>

this returns an array item[] that holds the ids of all the items.

Now the real issue:

1. The above loop will same ids to ‘n’ items
2. All ids will be ‘item[]’

Both of the above points are not XHTML Strict 1.0 valid and hence I am  
seeking a solution.

An alternative could be like I assign each check box a unique id like  
this:

\<% items.each do |item| %\>  
\<%= check\_box\_tag “item\_#{item.id}”, “#{item.id}” %\>  
\<% end -%\>

But this will involve an overhead of collecting all ids in controller.

Any new ideas ??

~rd

---

<div class="post-metadata">

**Author:** ![sazwqa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@sazwqa](https://www.ruby-forum.com/u/sazwqa)\
**Post date:** [February 1, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697/2 "2008-02-01T22:30:45Z")

</div>

On Jan 31, 2008 7:35 AM, Saz W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. The above loop will same ids to ‘n’ items
> 2. All ids will be ‘item[]’
> 
> Both of the above points are not XHTML Strict 1.0 valid and hence I am  
> seeking a solution.

gsub to replace the square brackets with something else?

> An alternative could be like I assign each check box a unique id like  
> this:
> 
> \<% items.each do |item| %\>  
> \<%= check\_box\_tag “item\_#{item.id}”, “#{item.id}” %\>  
> \<% end -%\>
> 
> But this will involve an overhead of collecting all ids in controller.

You first example shows using item.id, so how is not also an overhead  
there? If you need the ids then you need the ids, overhead or not.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![sazwqa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@sazwqa](https://www.ruby-forum.com/u/sazwqa)\
**Post date:** [February 1, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697/3 "2008-02-01T22:57:20Z")

</div>

> 1. The above loop will same ids to ‘n’ items  
> \<% end -%\>
> 
> But this will involve an overhead of collecting all ids in controller.
> 
> Any new ideas ??

Yes. Use the same name attribute with [] and different ids:

\<% 5.times do |i| %\>  
\<%= check\_box\_tag “cb\_[#{i}]”, value=“#{i}”, false, :id =\> “cb\_#{i}” %\>  
\<% end %\>

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![sazwqa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@sazwqa](https://www.ruby-forum.com/u/sazwqa)\
**Post date:** [February 5, 2008, 11:46am UTC](https://www.ruby-forum.com/t/handling-multiple-check-boxes-in-a-xhtml-strict-way/127697/4 "2008-02-05T11:46:40Z")

</div>

Thanks a lot ! I got it solved.

So stupid of me to not note this fact that rails works by collecting  
‘name’ attribute and not ‘id’.

regards,  
~rd

Rimantas L. wrote:

> > 1. The above loop will same ids to ‘n’ items  
> > \<% end -%\>
> > 
> > But this will involve an overhead of collecting all ids in controller.
> > 
> > Any new ideas ??
> 
> Yes. Use the same name attribute with [] and different ids:
> 
> \<% 5.times do |i| %\>  
> \<%= check\_box\_tag “cb\_[#{i}]”, value=“#{i}”, false, :id =\> “cb\_#{i}” %\>  
> \<% end %\>
> 
> ## Regards, Rimantas
> 
> [http://rimantas.com/](http://rimantas.com/)
