title: Accessing Javascript variable from ruby
body: <p>Passing data from Ruby to Javascript is easy, for example:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      function change_value(val){
  
        alert ("&lt;%= @alert %&gt;")   
        }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>This will sent an alert with the data stored at the alert variable in the controller. </p>
  
  <p>But I don't know how it works in the opposite direction, for example, if I need to store an element <code>id</code> into a controllers variable:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      function change_value(element){
        &lt;% @element_id = *** element.id *** %&gt;
  
        alert ("&lt;%= @element_id %&gt;")   
        }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>The real deal is up next, the code surrended by *** are supposed to be the needed javascript values (@billeable_qts is a Hash):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
    function change_value(product){
  
      &lt;% @billeable_qts[****product.id****] = ***document.getElementById('product.id').value****  %&gt;
      &lt;% @billeable_qts.each do |key, value| %&gt;
         &lt;% alert = "Key = " + key + ", value: " + value.to_s%&gt;
         alert ("product: &lt;%= alert %&gt;")
      &lt;% end %&gt;
  
  
    }
  
  &lt;/script&gt;
  </code></pre>