# Button\_to\_function and javascript prompt?

**URL:** <https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003>\
**Category:** Rails\
**Created:** [October 12, 2007, 12:42am UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003 "2007-10-12T00:42:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 12, 2007, 12:42am UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003/1 "2007-10-12T00:42:12Z")

</div>

Hello,

I want to ask the users specific question using javascript, and then get  
the answer 🙂

\<%= button\_to\_function “My Test”, “prompt(‘Your name is?’)” %\>

How can I accomplish this?

I hope someone out there can help me with this, and thanks for your help  
😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 12, 2007, 1:33am UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003/2 "2007-10-12T01:33:57Z")

</div>

On 10/11/07, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I want to ask the users specific question using javascript, and then get  
> the answer 🙂
> 
> \<%= button\_to\_function “My Test”, “prompt(‘Your name is?’)” %\>
> 
> How can I accomplish this?

Well, that will do it. What do you want to do with the answer?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 12, 2007, 9:28am UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003/3 "2007-10-12T09:28:26Z")

</div>

Bob S. wrote:

> On 10/11/07, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello,
> > 
> > I want to ask the users specific question using javascript, and then get  
> > the answer 🙂
> > 
> > \<%= button\_to\_function “My Test”, “prompt(‘Your name is?’)” %\>
> > 
> > How can I accomplish this?
> 
> Well, that will do it. What do you want to do with the answer?

I need to catch the answer and add it to the database 😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 12, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003/4 "2007-10-12T17:00:46Z")

</div>

Thank you very much Bob S. 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [October 12, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/button-to-function-and-javascript-prompt/117003/5 "2007-10-12T15:09:52Z")

</div>

On 10/12/07, Jamal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > How can I accomplish this?
> > 
> > Well, that will do it. What do you want to do with the answer?
> 
> I need to catch the answer and add it to the database 😃

I suggest you put the results of prompt() into a (js) variable and  
then pass it to the server through an Ajax.Request. You can use the  
Rails remote\_function() helper to write the js for that. Use the :with  
option to pass the js variable.

Here’s an example from an app of mine:

\<%= javascript\_tag %Q{  
function x\_add\_task()  
{  
var name = prompt(‘Name for new task’, ‘’);  
if (name == null) return;  
#{remote\_function :url =\> {:action =\> ‘x\_add\_task’, :id =\>  
@obj.id}, :with =\> “‘name=’ + name” }  
}  
} %\>

Then later:

\<%= link\_to\_function ‘Add Task’, ‘x\_add\_task()’ %\>
