Display text_field if checkbox checked


#1

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


#2

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’) %>


#3

Wow, thank you so very much. That worked!!!

Thank you again, this will come in very handy!

Mike


#4

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>


#5

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