views:

291

answers:

3

I'm trying to figure out how to use RJS to swap between two fields... i know how to replace the values but I can't seem to figure out how to read it.

Is it not possible to read values thru RJS? Only to replace?

<%= link_to_function "Swap" do |page| 
      #to_value = page[:currency_to].value
      page[:currency_from].value = to_value
      page[:currency_to].value = "test"
     end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001664

A: 

I don't know much about prototype or rjs, but a quick look at the prototype API shows a getValue(). I would think this has to do with page[:currency_to] returning a prototype element as opposed to just a DOM element.

Maybe try replacing

to_value = page[:currency_to].value

with

to_value = page[:currency_to].getValue()

Can you just use whatever javascript you want in rjs files? My guess is no, since it's Ruby 'rendering' Javascript.

The section of the Rails API you linked to is a bit misleading I think. They don't seem to be referring to an actual 'value', they just use value as the name to be passed to the block.

theIV
yeah... no it doesn't seem to work. its very easy to set things and make things happen, but reading things seems difficult. i can't seem to find any examples.
holden
+1  A: 

I tested both of the following.

<%= link_to_function "Swap", 'var tmp_frm = $("#currency_from").val();
                              var tmp_to = $("#currency_to").val();
                              $("#currency_from").val(tmp_to);
                              $("#currency_to").val(tmp_frm);'%>

or better yet

<%= link_to_function "Swap", 'var tmp_frm = $("#currency_from").val();
                              $("#currency_from").val($("#currency_to").val());
                              $("#currency_to").val(tmp_frm);'%>

depending on your html, you may need to remove the pound symbols

<%= link_to_function "Swap", 'var tmp_frm = $("currency_from").value;
                                      var tmp_to = $("currency_to").value;
                                      $("currency_from").value = tmp_to;
                                      $("currency_to").value = tmp_frm;'%>
jrhicks
Ah, great. That did it. or almost, the idea was right but for some reason neither of those worked, I had to slightly modify it. Thanks!
holden
A: 
<%= link_to_function "Swap", 'var tmp_frm = $("currency_from").value;
                                   var tmp_to = $("currency_to").value;
                                   $("currency_from").value = tmp_to;
                                   $("currency_to").value = tmp_frm;'%>
holden