# Dynamically generate a field option from dropdown selection

**URL:** <https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368>\
**Category:** Rails\
**Created:** [January 22, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368 "2009-01-22T15:13:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 22, 2009, 3:13pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/1 "2009-01-22T15:13:50Z")

</div>

Hi all,

I’m having a hard time trying to find a good tutorial on how to generate  
a field (with ajax) from a dropdown selection.

For example:

I have an app where you can select up to two tickets per person. Only if  
the user selects ‘2’ from the dropdown menu should a field pop  
up/generate below the dropdown and ask the user for the name of the  
second person.

So far I have the following code:

### Number of Tickets?
\<%= select :tickets, :quantity, { "Select quantity" =\> "", "1" =\> "1", "2" =\> "2"} %\>
### Name of guest?
 \<%= f.text\_field :guest\_name %\>

So in my case, only if quantity is selected as two should guest\_name  
appear (without refreshing the page).

Thanks in advance and THANK YOU FOR YOUR TIME!!

Best regards,  
Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 22, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/2 "2009-01-22T20:41:14Z")

</div>

On Thu, Jan 22, 2009 at 9:13 AM, Tony T.  
\<[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> wrote:

> up/generate below the dropdown and ask the user for the name of the
> 
> So in my case, only if quantity is selected as two should guest\_name  
> appear (without refreshing the page).
> 
> Thanks in advance and THANK YOU FOR YOUR TIME!!

I was looking for an answer to much the same question not too long ago.  
I  
found all sorts of tutorials showing how to populate a 2nd drop down  
menu  
based upon the contents of the first (such as  
[PullMonkey Blog » Blog Archive » Dynamic Select Boxes – Ruby on Rails](http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails)).  
Ultimately, I was able to learn enough from reading those to learn that  
(in  
your case) I would want to put a blank

 tag in my layout where I  
would  
want the “name of guest” stuff to appear, and to populate the “name of  
guest” stuff only when quantity == 2.

I hope this helps.

–wpd

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 23, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/3 "2009-01-23T16:16:38Z")

</div>

Patrick D. wrote:

> Ultimately, I was able to learn enough from reading those to learn that  
> (in  
> your case) I would want to put a blank
> 
> tag in my layout where I  
> would  
> want the “name of guest” stuff to appear, and to populate the “name of  
> guest” stuff only when quantity == 2.
> 
> I hope this helps.
> 
> –wpd

Thanks for the tip Patrick.

I saw the article and I understand how it’s SUPPOSED to work. I also saw  
a railscast about the same topic (dynamic drop down which fills a second  
drop down). Unfortunately my javascript skills are not so great, so it’s  
a challenge to make it work. I’m sure with some more time I’ll get it  
working.

If anyone else can offer some help I’d appreciate it. Thanks again!

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 24, 2009, 12:08pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/4 "2009-01-24T12:08:58Z")

</div>

I’ve just been doing something similar. This works, but might not be  
as Railsy as it could be:

### Number of Tickets?
\<%= select :tickets, :quantity, {"1" =\> "1", "2" =\> "2"}, {:prompt =\> 'Select quantity...'}, {:onchange =\> "if(this.options [this.selectedIndex].value=='2') {Effect.BlindDown('guest\_name', {duration: 0.5}); return false;} else {Effect.BlindUp('guest\_name', {duration: 0.5}); return false;}"} %\>

### Name of guest?
 \<%= f.text\_field :guest\_name %\>

Notes:

1. This is not using Ajax - it is using JavaScript to show and hide a  
div based on a selected value. You’re not fetching data, so why use  
Ajax to go to the server in this case?
2. You need to have Scriptaculous installed to use the effects (good  
idea to give feedback to the user?). If you have \<%=  
javascript\_include\_tag :defaults %\> in the head of your html page then  
Scriptaculous is good to go.
3. Even if the guest\_name field is hidden, it will still be submitted,  
so test the value from your quantity select before saving the value  
from guest\_name.
4. I made a small change to your select to get your prompt (‘Select  
quantity’) to work correctly.
5. Be careful with the formatting of the onchange part when copying  
the code here: there should be new lines before {Effect.BlindDown,  
else, and {Effect.BlindUp, but nowhere else.

Good luck.

On Jan 23, 3:13 am, Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 25, 2009, 12:51am UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/5 "2009-01-25T00:51:14Z")

</div>

Tony T. wrote:

> I even tried doing an  
> if statement in the div: \<div id=“guest\_name”\<%= " style=‘hidden’" if  
> !@tickets.quantity == ‘2’ %\>\>

Okay, obviously there was a typo which when fixed ended up working.

style=“display:none;” not style=“hidden”.

Hope this helps someone.

-Tony

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 25, 2009, 10:33am UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/6 "2009-01-25T10:33:02Z")

</div>

On Jan 25, 11:45 am, Tony T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks Chris! I was able to get it working!

You’re very welcome. I’ve received help here, so hope to give it when  
I can. And while we’re talking about that, have we thanked the  
wonderful Fred Cheung this week? Fred: you’re a gem.

> Two issues I’m having with this implementation though:
> 
> First, if I go to edit the form, I can see the ‘2’ is selected but the  
> guest\_name field is hidden until I select a different number of tickets  
> (I guess this activates/reloads the javascript). I even tried doing an  
> if statement in the div: \<div id=“guest\_name”\<%= " style=‘hidden’" if  
> !removed\_email\_address@domain.invalid == ‘2’ %\>\> which works (I can see this from the page  
> source). However, the div is ALWAYS visible, until I select a different  
> number of tickets and it acvtivates/reloads the javascript. Any ideas on  
> how to get this to work?

Good point about the div not being visible when editing the form. I’d  
better go and check my own code…

> Second, I ended up using a table structure for the form, wrapping the  
> DIV around the doesn’t work (the guest\_name field is always  
> visible). I’m guessing this is a table structure issue so I need to take  
> a more in depth look.

I started off using tables for forms too, but was dissatisfied because  
a table is supposed to be a way of showing tabular information (i.e.  
rows of similar data) and most forms don’t fall in that category.  
Tables are a pain when you want to do this kind of thing because of  
some browser-specific issues (Internet Explorer…). There’s a  
discussion on Rails Forum about using css (and not tables) to lay out  
forms: [http://railsforum.com/viewtopic.php?id=25297](http://railsforum.com/viewtopic.php?id=25297)

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [September 18, 2015, 11:59am UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/7 "2015-09-18T11:59:59Z")

</div>

HI every one I have a three dropdown list structure  
in which first is country second is states and third is cities.States  
populates correctly on the basis of country but city dosen’t.  
what steps to be taken while doing it.  
plz help me out

---

<div class="post-metadata">

**Author:** ![bagadoughnuts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@bagadoughnuts](https://www.ruby-forum.com/u/bagadoughnuts)\
**Post date:** [January 24, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/dynamically-generate-a-field-option-from-dropdown-selection/158368/8 "2009-01-24T23:45:41Z")

</div>

Chris B. wrote:

> I’ve just been doing something similar. This works, but might not be  
> as Railsy as it could be:
> 
> ### Number of Tickets?
> \<%= select :tickets, :quantity, {"1" =\> "1", "2" =\> "2"}, {:prompt =\> 'Select quantity...'}, {:onchange =\> "if(this.options [this.selectedIndex].value=='2') {Effect.BlindDown('guest\_name', {duration: 0.5}); return false;} else {Effect.BlindUp('guest\_name', {duration: 0.5}); return false;}"} %\>
> 
> ### Name of guest?
> \<%= f.text\_field :guest\_name %\>

Thanks Chris! I was able to get it working!

You’re right about the Ajax not being needed, I’m just so used to  
thinking that anything that “pops up” is ajax, even though it doesn’t  
need to be a server request. Sorry about that.

Two issues I’m having with this implementation though:

First, if I go to edit the form, I can see the ‘2’ is selected but the  
guest\_name field is hidden until I select a different number of tickets  
(I guess this activates/reloads the javascript). I even tried doing an  
if statement in the div: \<div id=“guest\_name”\<%= " style=‘hidden’" if  
!@tickets.quantity == ‘2’ %\>\> which works (I can see this from the page  
source). However, the div is ALWAYS visible, until I select a different  
number of tickets and it acvtivates/reloads the javascript. Any ideas on  
how to get this to work?

Second, I ended up using a table structure for the form, wrapping the  
DIV around the doesn’t work (the guest\_name field is always  
visible). I’m guessing this is a table structure issue so I need to take  
a more in depth look.

Thanks again for the code Chris! It was extremely helpful.

-Tony
