Forum: Ruby on Rails Display text_field if checkbox checked

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 wiz561@gmail.com (Guest)
on 2007-08-02 18:32
(Received via mailing list)
Hi!

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.


Thanks,
mike
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (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') %>
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 wiz561@gmail.com (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
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 wiz561@gmail.com (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
5cfe2c740d00a1b243e93ed79fdf3a1b?d=identicon&s=25 wiz561@gmail.com (Guest)
on 2007-08-03 18:43
(Received via mailing list)
For others, I figured it out.  It seems to work the same...

---
Response:
    <%= 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 %>
    </div>
----
This topic is locked and can not be replied to.