# Cannot toggle div from radio\_buttons

**URL:** <https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391>\
**Category:** Rails\
**Created:** [May 12, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391 "2008-05-12T19:09:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e57/32.png) [@Erwin](https://www.ruby-forum.com/u/Erwin)\
**Post date:** [May 12, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391/1 "2008-05-12T19:09:16Z")

</div>

Hi

I have a couple of radio\_buttons in my form

\<% form\_for …

and I try to toggle the display of a specific div, using :onclick  
(1st radio hide, 2nd radio display) the div  
but i cannot get it right (tried various solution with js, but  
without any success…)

| \<%= radio\_button\_tag('contact\_type', '1', (@contact\_type == "1" ? true : false), {:id =\> 'ct1', :onclick =\> "$('contact\_by').toggle;"}) -%\> Contact 1 \<%= radio\_button\_tag('contact\_type', '0', (@contact\_type == "0" ? true : false), {:id =\> 'ct2', :onclick =\> "$('contact\_by').toggle;"}) -%\> Contact 0 |
| 

contacted by ... textfield to be inserted there

\</div
 |

div class="buttons"\> \<input class="button"... submit button area...

\<% end %\>

need some help … thanks a lot

---

<div class="post-metadata">

**Author:** ![Erwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e57/32.png) [@Erwin](https://www.ruby-forum.com/u/Erwin)\
**Post date:** [May 12, 2008, 8:50pm UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391/2 "2008-05-12T20:50:50Z")

</div>

Not sure if you’ve got a typo there or not, but toggle is a js  
function so you’ll need parens:

:onclick=\>“$(‘contact\_by’).toggle();”

[http://prototypejs.org/api/element/toggle](http://prototypejs.org/api/element/toggle)

Just a word of warning… since you have two different elements  
calling toggle via onclick, you may have a different user experience  
than you expect. Specifically, clicking the first radio button twice  
you will toggle the div visible and then invisible. You will have  
more consistent results if you use hide() and appear() explicitly.

---

<div class="post-metadata">

**Author:** ![Erwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e57/32.png) [@Erwin](https://www.ruby-forum.com/u/Erwin)\
**Post date:** [May 13, 2008, 11:11am UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391/3 "2008-05-13T11:11:06Z")

</div>

Unfortunatly, even with $(‘mydiv’).show() and $(‘mydiv’).hide() , it  
doesn’t work  
1- the div is displayed in a glance then hidden  
2- the radio button doesn’t switch at all…

---

<div class="post-metadata">

**Author:** ![Erwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e57/32.png) [@Erwin](https://www.ruby-forum.com/u/Erwin)\
**Post date:** [May 13, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391/4 "2008-05-13T16:40:14Z")

</div>

Any chance that you have a css class on the div that is also setting  
it to display:none? Prototype cannot override that.

---

<div class="post-metadata">

**Author:** ![Erwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a88e57/32.png) [@Erwin](https://www.ruby-forum.com/u/Erwin)\
**Post date:** [May 12, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/cannot-toggle-div-from-radio-buttons/137391/5 "2008-05-12T22:51:54Z")

</div>

thanks alot… (yes… I forgot the () js function… )  
I’ll do a try, with appear and hide (or show hide ?)
