Dynamically hiding/toggling a textfield with a drop down select box using rails and javascript
This comes from this post on railsforum.
First, make sure you can do the basic javascript. See the pastie.
As you can see we are just going to use a basic toggle. Let’s keep things simple to start.
Now make sure you’re capable of doing a basic select box with selected working.
<%= f.label :redirect_locale, 'Page redirect:' %>
<%= select("page", "redirect_locale", {
"Yes, to posts. (works like a blog or news)" => 1,
"No. (keep as a normal page)" => 0,
}, { :selected => @page.redirect_locale })%>
Now combine what you just did, but let’s use show and hide instead of toggle
<%= f.label :redirect_locale, 'Page redirect:' %>
<%= select("page", "redirect_locale",
{"Yes, to posts. (works like a blog or news)" => 1, "No. (keep as a normal page)" => 0,},
{ :selected => @page.redirect_locale },
{ :onchange => "if (this.value == '1') { $('body_toggle').hide(); $('sidebar_toggle').hide() } else { $('body_toggle').show(); $('sidebar_toggle').show(); }" })%>
That should work as long as you have some divs or spans or p with the id=”sidebar_toggle” and id=”body_toggle”
