RJS and drop down menu

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! :slight_smile:

Hi Matt,

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! :slight_smile:

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 :wink:

HTH, Bill

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.

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.

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.

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