Topic: simple ajax/javascript

Hey, I have (what I assume) is a very simple question.

I have a form that I want to add some dynamic functionality to.  I'm also know almost hardly any javascript, but I assume that there are Rails helpers/ways of doing for this.

At the beginning of the form, I have a yes/no select field that defaults to "no".  If the user selects "yes" however, I would like an additional form field (that was not there before) to appear below it.

That's pretty much it.  Any suggestions?

Re: simple ajax/javascript

Most of the Rails helpers are for AJAX commands. But, since this isn't AJAX, you will want to do it through just Javascript on the client side. But all you should need is an if condition to determine the selected menu then show the other fields. Sorry I can't provide a code snippet without testing.

Railscasts - Free Ruby on Rails Screencasts

Re: simple ajax/javascript

Oh hey guys...

Based on the little story you gave us, I believe you're looking for this helper:

Element.toggle('element-id' ); /* toggles element-id */

I think you need to be able to toggle between yes / no, hence the above helper.
If you don't want users to toggle between yes / no, replace 'toggle' with 'show', so it would be Element.show instead.
You would need to set this helper on both the yes and no select fields to toggle back and forth.

You also want the element-id to be hidden at first, because 'no' is selected as default. So go with this:

<input type="text" name="bla" id="element-id" style="display: none;">

What Element.toggle would do is use the above id and change the style display to 'block', so it would become visible. And then vice versa, hence the toggling.


Good luck!

Last edited by maestro (2006-12-28 13:51:30)