# Checkboxes expand to show new options when checked?

**URL:** <https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425>\
**Category:** Rails\
**Created:** [March 31, 2006, 4:44am UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425 "2006-03-31T04:44:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 31, 2006, 4:44am UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/1 "2006-03-31T04:44:42Z")

</div>

I’m writing a simple CMS where every item (“listing”) HABTM categories  
and subcategories (which themselves belong\_to a category). What I’m  
trying to do now is create a UI effect for listing creation: when you go  
to create a listing, only the possible categories will be shown, but  
when you select a category a set of new categories will be displayed  
like so:

Lions  
Tigers  
Bears

> click on Tigers

Lions  
Tigers  
Feeding  
Breeding  
Storing  
Bears

> de-select Tigers and it returns to normal.

What I have right now is a display where every category and subcategory  
is displayed at once. The code looks something like this:

\<% for category in @categories %\>

 \<%= category.name %\>
 \<% for subcategory in category.subcategories %\> 
 \<%= subcategory.name %\> \<% end %\>

Is there some kind of conditional I can put before the second “for”  
statement? An is\_checked? sort of thing? Or maybe AJAX using  
Element.toggle? I don’t know where to start - and a web search didn’t do  
much good.

Thanks,

Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 31, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/2 "2006-03-31T20:24:01Z")

</div>

Still curious about this…

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 31, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/3 "2006-03-31T21:05:07Z")

</div>

you have the javascript option, or the ajax option.

In javascript, it is quite straightforward: a click on something toggle  
the  
visibility of a div. It works like that:

\<% for category in @categories %\>

\<%= category.name %\>
 \<% for subcategory in category.subcategories %\> 
 \<%= subcategory.name %\>
\<% end %\> \<% end %\>

IMHO, ajax would be useful here only if subcategory is big. Otherwise,  
this  
method work fine.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 1, 2006, 12:25am UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/4 "2006-04-01T00:25:39Z")

</div>

I’m having one problem. In my code, inside the \<input … \> tag I have:

\<% if @listing.categories.include? category %\>checked=“checked”\<% end %\>

because the same form that’s used to create is used to edit. Obviously  
this results in the categories being checked, but the subcategories  
aren’t toggled on. I tried this inside the

:

\<% if @listing.categories.include? category %\>

\<% end %\>

but it didn’t togggle…

Sorry, I’m still a newbie at all of this.

Thanks again,

Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/5 "2006-04-01T10:52:04Z")

</div>

Your problem is that you would like a category checked to be shown,  
right?  
then the code

Should be replaced by
 \>

Regards

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 1, 2006, 10:52am UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/6 "2006-04-01T10:52:04Z")

</div>

Ops, I meant:

 \>

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 31, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/7 "2006-03-31T23:42:33Z")

</div>

> IMHO, ajax would be useful here only if subcategory is big. Otherwise,  
> this  
> method work fine.

That works great! Thanks a lot,

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [April 1, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/checkboxes-expand-to-show-new-options-when-checked/56425/8 "2006-04-01T21:48:13Z")

</div>

Nicolas B. wrote:

> Your problem is that you would like a category checked to be shown,  
> right?  
> then the code
> 
> Should be replaced by
> \>
> 
> Regards
> 
> Doh! It seems so obvious once I see it. 🙂
> 
> Thanks a lot,
> 
> Adam
