# RJS and drop down menu

**URL:** <https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046>\
**Category:** Rails\
**Created:** [June 25, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046 "2009-06-25T18:31:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fireknite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fireknite](https://www.ruby-forum.com/u/fireknite)\
**Post date:** [June 25, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046/1 "2009-06-25T18:31:54Z")

</div>

Trying to update a text field on a page with the value from a drop down  
select menu. Having a hard time accessing the value in the menu. Any  
help would be great! 🙂

---

<div class="post-metadata">

**Author:** ![fireknite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fireknite](https://www.ruby-forum.com/u/fireknite)\
**Post date:** [June 25, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046/2 "2009-06-25T18:56:41Z")

</div>

Hi Matt,

On Thu, 2009-06-25 at 18:31 +0200, Matt Y. wrote:

> Trying to update a text field on a page with the value from a drop down  
> select menu. Having a hard time accessing the value in the menu. Any  
> help would be great! 🙂

The basics are to put an observe\_field on the drop down with a url that  
does whatever processing you need to do to construct the update you want  
to do and either a) use the :update option for observe\_field, or b) use  
RJS to do the update. The :update option limits you to a single DOM  
element in terms of what can be updated. Say more to get more 😉

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![fireknite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fireknite](https://www.ruby-forum.com/u/fireknite)\
**Post date:** [June 25, 2009, 7:10pm UTC](https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046/3 "2009-06-25T19:10:44Z")

</div>

Bill,  
I have a collection select that shows a list of users:  
\<%= collection\_select(:person, :convert, @nonUsers, :id, :fullName,  
{:prompt =\> “Choose…”}, :style =\> “width:125px;”) %\>

And a text field:  
\<%= text\_field\_tag :userName %\>

When a user is selected I’m trying to pull that user out of a array  
variable that I already have set up, @nonUsers (used to populate the  
collection\_select), and have that user’s userName put into the text box.

I can’t seem to figure out how to access the value of what’s been  
selected with an observe\_field.

(Trying to do all this with RJS and not make a post back to the server).

My observe\_field looks like:  
\<%= observe\_field “person\_convert”, :function =\> update\_page {|page|  
page[“userName”].value = @nonUsers.select{|p| p.id == \*\*\*\*\* }.collect{  
|p| p.fullName } }%\>

BUT I’m not sure what to put in place of the \*\*\*\*\* to get that  
collection\_select’s selected value.

---

<div class="post-metadata">

**Author:** ![fireknite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6bbea6/32.png) [@fireknite](https://www.ruby-forum.com/u/fireknite)\
**Post date:** [June 25, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/rjs-and-drop-down-menu/171046/4 "2009-06-25T20:25:55Z")

</div>

Hi Matt,

I haven’t used the :function option before. I find it easier just to  
write the JS when I want to keep it all in the browser, so take what  
follows with a grain of salt.

On Thu, 2009-06-25 at 19:10 +0200, Matt Y. wrote:

> collection\_select), and have that user’s userName put into the text box.  
> That’s not what you want to do. observe\_field will capture / pass  
> selected value.

> I can’t seem to figure out how to access the value of what’s been  
> selected with an observe\_field.
> 
> (Trying to do all this with RJS and not make a post back to the server).

Personally, I’d do it against the server first (because it’s  
oh-so-simple) and leave the optimization until performance is an issue.

> My observe\_field looks like:  
> \<%= observe\_field “person\_convert”, :function =\> update\_page {|page|  
> page[“userName”].value = @nonUsers.select{|p| p.id == \*\*\*\*\* }.collect{  
> |p| p.fullName } }%\>

> BUT I’m not sure what to put in place of the \*\*\*\*\* to get that  
> collection\_select’s selected value.

If I’m reading the api / code for observe\_field right, I’m not sure  
the :function variation supports what you want to do. As best I can  
tell (having not much time to review the Rails source), if it does, the  
final form would, I think, look something like this:

\<%= observe\_field(“person\_convert”, :function =\> “update\_page(\*\*\*\*\*)”}%\>

But in the build\_observer source, it doesn’t look like there’s a way to  
pass the value in. Looks like it’s just passing the  
“update\_page(\*\*\*\*\*)” without any further processing.

I know that’s not much help. Sorry. My recommendation would be to  
repost your problem under a more descriptive Subject line. Something  
like ‘problem passing selected value to observe\_field :function’

Best regards,  
Bill
