Forum: Ruby on Rails Display text_field if checkbox checked

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms. (Guest)
on 2007-08-02 18:32
(Received via mailing list)

I've been trying to figure out how to display a text_field if a
checkbox on my form is checked.  If the checkbox is unchecked, the
text_field is removed.

If anybody has an example or tutorial on this, that would be great.
I'm going to do some more searching in google now and not the
group...but if anybody can help, that would be awesome.

89bf722038e07b8fba56d96df40b6267?d=identicon&s=25 Jeff E. (jemminger)
on 2007-08-03 04:12
(Received via mailing list)
something like this ought to do it:

<%= check_box :foo, :box, :onclick =>
"document.getElementById('foo_bar').style.display = (this.checked ?
'inline' : 'none');" %>
<%= text_field :foo, :bar, :style => ((params[:foo] and params[:foo]
[:box] == '1') ? 'display:inline' : 'display:none') %> (Guest)
on 2007-08-03 16:38
(Received via mailing list)
Wow, thank you so very much.  That worked!!!!

Thank you again, this will come in very handy!

Mike (Guest)
on 2007-08-03 16:55
(Received via mailing list)
I don't mean to be a pain...i'm really greatful that you took the time
to help me out.

But, is there also a way I can display a snippet of text when the
text_field pops up as well?

thanks again for your help!
mike (Guest)
on 2007-08-03 18:43
(Received via mailing list)
For others, I figured it out.  It seems to work the same...

    <%= check_box :response, :sens_class, :onclick =>
    "document.getElementById('response_cscs').style.display =
(this.checked ?
    'inline' : 'none');" %>

    <div id="response_cscs" style="display:none">
      CSCS #: <%= text_field :response, :cscs %>
This topic is locked and can not be replied to.