title: Rails passing value to hidden field using javascript
body: <p>I have an array structured as follows:</p>
  <pre><code>&lt;% @locations = [['Town 1',2],['Town 2',2]...] %&gt;
  </code></pre>
  <p>That I pass through an <code>options_for_select</code> tag that passes the shows the town name correctly and passes the id when submitted which is great. But, what I want to do is pass the town name (key?) as well to display on the results page as well. I understand the best method is to use a <code>hidden_field_tag</code> with javascript to pass the value through, but all I can pass is the id value, not the town name value.</p>
  <p><strong>Form</strong></p>
  <pre><code>&lt;%= form_tag(results_path, method: :post) do %&gt;
    &lt;%= select_tag :location, options_for_select(@locations) %&gt;
    &lt;%= hidden_field_tag(:location_name, value = nil, html_options = {id: 'locationName'}) %&gt;
    &lt;%= submit_tag 'Submit' %&gt;
  &lt;% end %&gt;
  </code></pre>
  <p><strong>Javascript</strong></p>
  <pre><code>$('#location').change(function(){
    var input = document.getElementById(&quot;location&quot;);
    var site = document.getElementById(&quot;locationName&quot;);
    site.value = input.value;
  })
  </code></pre>