# Toggle

**URL:** <https://discuss.rubyonrails.org/t/toggle/35992>\
**Category:** rubyonrails-talk\
**Created:** [November 3, 2008, 2:52pm UTC](https://discuss.rubyonrails.org/t/toggle/35992 "2008-11-03T14:52:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 3, 2008, 2:52pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/1 "2008-11-03T14:52:24Z")

</div>

Hi!

I'am using Effect.toggle to show/hide a div field. Is there a simple way to also toggle the icon i am using. (+ if hidden and - if visible)

\<%= link\_to\_function '', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect.toggle('summary\_dropdown')", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:class =\> 'icon-expand' %\>

Thanks.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 3, 2008, 3:03pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/2 "2008-11-03T15:03:06Z")

</div>

> Hi!
> 
> I'am using Effect.toggle to show/hide a div field. Is there a simple  
> way to also toggle the icon i am using. (+ if hidden and - if visible)

I typically do that by also toggling some css class (eg $ ('something').toggleClassName('expanded'). in this case you can  
probably do this.toggleClassName('some-class') )

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 4, 2008, 9:02am UTC](https://discuss.rubyonrails.org/t/toggle/35992/3 "2008-11-04T09:02:25Z")

</div>

Thanks for the quick reply. But where am i going to put this?

> I typically do that by also toggling some css class (eg $ ('something').toggleClassName('expanded'). in this case you can probably do this.toggleClassName('some-class') )
> 
> Fred

\<%= link\_to\_function '', &nbsp;&nbsp;&nbsp;&nbsp;"Effect.toggle('summary\_dropdown')", &nbsp;&nbsp;&nbsp;&nbsp;:id =\> 'toggle', &nbsp;&nbsp;&nbsp;&nbsp;:class =\> 'icon-expand' %\>

I guess it should be someting like ('toggle').toggleClassName('icon-fade')

but where will i put this in my link\_to\_function ?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 4, 2008, 2:03pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/4 "2008-11-04T14:03:06Z")

</div>

I can only get one off them to work at a time. Do anyone know how to combine the two functions. So the div will fade/appear and the icon will change from + to -.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 4, 2008, 2:18pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/5 "2008-11-04T14:18:35Z")

</div>

> Thanks for the quick reply. But where am i going to put this?
> 
> > I typically do that by also toggling some css class (eg $ ('something').toggleClassName('expanded'). in this case you can probably do this.toggleClassName('some-class') )
> > 
> > Fred
> 
> \<%= link\_to\_function '', &nbsp;&nbsp;&nbsp;"Effect.toggle('summary\_dropdown')", &nbsp;&nbsp;&nbsp;:id =\> 'toggle', &nbsp;&nbsp;&nbsp;:class =\> 'icon-expand' %\>
> 
> I guess it should be someting like ('toggle').toggleClassName('icon-fade')
> 
> but where will i put this in my link\_to\_function ?

the second argument to link\_to\_function is an arbitrary javascript  
fragment. You can put as much in there as you want.

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 4, 2008, 2:57pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/6 "2008-11-04T14:57:48Z")

</div>

Frederick Cheung wrote:

> the second argument to link\_to\_function is an arbitrary javascript fragment. You can put as much in there as you want.
> 
> Fred

To give just a little more detail to what Fred said, you can do something like

"Effect.toggle('this-thing'); Effect.toggle('that-thing');"

Just remember that needs to be proper JavaScript since it's going to be eval'd.

Peace.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [November 4, 2008, 3:59pm UTC](https://discuss.rubyonrails.org/t/toggle/35992/7 "2008-11-04T15:59:39Z")

</div>

Got it working thanks for the reply.
