# How to toggle several divs at the click?

**URL:** <https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398 "2007-01-19T16:30:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398/1 "2007-01-19T16:30:05Z")

</div>

Hey all,  
I have several divs like this:

blabal

blabal

blabal

I would like to do a link\_to\_function or link\_to\_remote to make them  
visible all by one click.

Any idea how to do that?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [January 19, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398/2 "2007-01-19T16:51:01Z")

</div>

Patrick A. wrote:

> Hey all,  
> I have several divs like this:
> 
> blabal
> 
> blabal
> 
> blabal
> 
> I would like to do a link\_to\_function or link\_to\_remote to make them  
> visible all by one click.
> 
> Any idea how to do that?
> 
> thanx in advance
> 
> Pat

Use RJS.

myaction.rjs

page.select(‘div.collapseme’).each { |item| item.hide }

A.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 25, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398/3 "2007-09-25T22:36:45Z")

</div>

Patrick A. napisaÅ‚(a):

> Hey all,  
> I have several divs like this:
> 
> blabal
> 
> blabal
> 
> blabal
> 
> I would like to do a link\_to\_function or link\_to\_remote to make them  
> visible all by one click.
> 
> Any idea how to do that?

You can do this with ‘link\_to\_remote’ with the request to e.g. ‘change’  
action.  
But instead of ‘change.rhtml’ use ‘change.rjs’ template.  
Inside this template you can use e.g. ‘page.show’ for as many divs as  
you wish. You should give your divs ‘id’ parameter to reference them in  
‘page.show’ methods.

–  
RaW

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [September 25, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/how-to-toggle-several-divs-at-the-click/82398/4 "2007-09-25T22:51:36Z")

</div>

RJS Is not a must, you can use a JS onclick event.

link\_to ‘Open Divs’, “#”, {:onclick =\> 'Element.toggle(‘div1\_id’);  
Element.toggle(‘div2\_id’);}
