# Add text string to a database field when a button is pressed

**URL:** <https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576>\
**Category:** Rails\
**Created:** [March 21, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576 "2008-03-21T16:40:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 21, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576/1 "2008-03-21T16:40:52Z")

</div>

Hello I am new to ruby and rails (using rails 2). Can some body please  
help with the following -  
I have a form with a database text field and a series of buttons when a  
button is pressed i need a specific string with a time & date stamp  
inserting into the on screen text field.  
The problem is I don’t want the buttons to post the form, I want the  
form to stay open so the user can add additional notes to the same  
database text field.  
When they have finished then they can hit the create button to insert  
into the database their completed text.

How should I do this, code examples would be helpful as I am a newbie to  
programming with ruby, rails & javascript.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 21, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576/2 "2008-03-21T16:59:48Z")

</div>

Maybe this can help you:

[http://www.railsbrain.com/api/rails-1.2.3/doc/index.html?a=M001753&name=button\_to\_function](http://www.railsbrain.com/api/rails-1.2.3/doc/index.html?a=M001753&name=button_to_function)

for the javascript function this should do it:

$(‘id\_of\_your\_text\_field’).value = ‘your string’

Stijn  
[http://www.xaop.com](http://www.xaop.com)

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 21, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576/3 "2008-03-21T20:54:14Z")

</div>

Stijn P. wrote:

> Maybe this can help you:
> 
> [http://www.railsbrain.com/api/rails-1.2.3/doc/index.html?a=M001753&name=button\_to\_function](http://www.railsbrain.com/api/rails-1.2.3/doc/index.html?a=M001753&name=button_to_function)
> 
> for the javascript function this should do it:
> 
> $(‘id\_of\_your\_text\_field’).value = ‘your string’
> 
> Stijn  
> [http://www.xaop.com](http://www.xaop.com)

Hello Stijn

Thanks for helping,  
I have looked at the button\_to\_function help doc and searched google  
etc. and because I am a total newbie it dosen’t make much sence to me  
with regards to the implementation of the javascript side of things.

so I have a few questions I was hoping you could help with…

1. Do I put the javascript in the controller.rb as a ‘def’ or does it  
go in my html.erb file? - do you have a sample source code example  
please :)?

2. The (‘id\_of\_your\_text\_field’) - how do I know what my text field id  
is, for example I have the following extracted from my html.erb file…

so would my id of my text be - ‘action\_log’ ?

1. I was wondering if the user typed some text in the field and then  
clicked the button would the users text be wipped out of the field by  
the button action? - that would be a problem.

thanks

phil

---

<div class="post-metadata">

**Author:** ![technomancy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b77776/32.png) [@technomancy](https://www.ruby-forum.com/u/technomancy)\
**Post date:** [March 22, 2008, 10:48am UTC](https://www.ruby-forum.com/t/add-text-string-to-a-database-field-when-a-button-is-pressed/132576/4 "2008-03-22T10:48:54Z")

</div>

ok Thanks to Stijn and this website…

> **[JavaScript | tutorials | Textbox](http://www.developnew.com/javascript/textbox.php)**
>
> Javascipt Textbox Tutorials, Textbox javascript tutorials, Textbox , html Textbox, js Textbox, javascript Textbox value change, Textbox with button event, what is Textbox

I was able to work out how to do this, so here is my code which is found  
in my new.html.erb file in the views folder…

------------------------ javascript code  
function Assign()  
{  
document.getElementById(“logger\_action\_log”).value =  
document.getElementById(“logger\_action\_log”).value + “Here is my  
output message.”;  
}

------------------------- button code

\<%= button\_to\_function “Greeting”, “Assign()” %\>

* * *

to get the ‘logger\_action\_log’ id i just looked at my page source code  
from the web browser.

so its easy when you know how, hope somebody else finds this useful, let  
us know if you do.

Phil
