# Send Java script variable to a controller

**URL:** <https://www.ruby-forum.com/t/send-java-script-variable-to-a-controller/185366>\
**Category:** Rails\
**Created:** [March 18, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/send-java-script-variable-to-a-controller/185366 "2010-03-18T20:50:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [March 18, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/send-java-script-variable-to-a-controller/185366/1 "2010-03-18T20:50:12Z")

</div>

I am trying to build an application that has google maps. Inside the  
script there are two java script variables. I want to send those two  
variables using a java function. What is the best why to do that.  
Could someone give me a simple example on that.

---

<div class="post-metadata">

**Author:** ![mohammed\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bcef8e/32.png) [@mohammed\_a](https://www.ruby-forum.com/u/mohammed_a)\
**Post date:** [March 19, 2010, 9:35am UTC](https://www.ruby-forum.com/t/send-java-script-variable-to-a-controller/185366/2 "2010-03-19T09:35:47Z")

</div>

Here is non ajax method(but ajax pretty the same):  
var f = document.createElement(‘form’);  
f.style.display = ‘none’;  
this.parentNode.appendChild(f);  
f.method = ‘POST’; #or get  
f.action = this.href;  
var m = document.createElement(‘input’);  
m.setAttribute(‘type’, ‘hidden’);  
m.setAttribute(‘name’, ‘\_method’);  
[//m.setAttribute](https://m.setAttribute)(‘value’, ‘delete’); #or put it depends of your needs  
f.appendChild(m);  
//Now we can send your variables:  
var s = document.createElement(‘input’);  
s.setAttribute(‘type’, ‘hidden’);  
s.setAttribute(‘name’, ‘my\_var’);  
s.setAttribute(‘value’, ‘my\_var\_value’);  
f.appendChild(s);  
f.submit();
