Spyros
Spyros

Reputation: 48636

Ajax Dropdown Selection to change TextArea Contents

I'm trying to use Ajax in one of my Rails applications to have a form_tag textarea change its contents according to the selected value of a dropdown that is out of that form_tag.

I would like to ask, what is the correct way of handling this ? Is it possible to respond to js in my show action and have a js.rjs ? Do you happen to know of any resources or can offer some insight ?

Upvotes: 0

Views: 504

Answers (1)

Matzi
Matzi

Reputation: 13925

You should write a javascript, which triggers on the dropdown menu's onchange event, and start an ajax process. With jQuery it is something like this (in your show code within a script tag):

$("#dropdownMenuName").change(function(){
  $.get("controller/action.txt", function(data){ $("#textareaName").val(data); } );
});

This simply sends a request to your app on the controller/action.txt action, and the result is pasted into the textarea's text property. Of course you should write the answer as a simple text, as the result is printed in the textarea instantly.

Upvotes: 2

Related Questions