# Insert text into text area field with RJS

**URL:** <https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072>\
**Category:** Rails\
**Created:** [March 25, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072 "2007-03-25T20:10:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 25, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/1 "2007-03-25T20:10:09Z")

</div>

Hi,

I’d like to insert/append some default text into a text area after  
clicking on an image button ‘test.png’ in edit.rhtml:

\<%= link\_to image\_tag(“test.png”), url =\> { :action =\> :new\_text } %\>

Action:

# article\_body is the textarea’s id.

def new\_text  
render :update do |page|  
page.insert\_html :bottom, :article\_body, “text\n”  
end  
end

Not surprisingly insert\_html doesn’t work properly in this innerHTML  
context. But how it is possible to append text to existing one?

page[:article\_body][:value] = “text”

e.g. overwrites the old content completely.

Do I need to use request.raw\_post or reques.query\_string in some way?

Best regards  
Ingo

---

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 25, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/2 "2007-03-25T21:31:07Z")

</div>

When in doubt, drop to javascript:

page \<\< “$(‘article\_body’).value += ‘blah’”

On Mar 26, 2:10 am, Ingo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 25, 2007, 11:23pm UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/3 "2007-03-25T23:23:21Z")

</div>

Thanks for this Eden,

just wondering:

page \<\< “$(‘article\_body’).value += ‘\n\blah\n’”

with some extra newlines and a backslash works in a javascript function:

function insertText(obj,myText) {  
[//obj.value](https://obj.value) += myText;  
$(‘article\_body’).value += ‘\n\blah\n’  
}

but not within a RJS environment (only tabs for example):

render :update do |page|  
page \<\< “$(‘article\_body’).value += ‘\n\blah\n’”  
end

Any ideas?

Regards  
Ingo

> When in doubt, drop to javascript:
> 
> page \<\< “$(‘article\_body’).value += ‘blah’”
> 
> On Mar 26, 2:10 am, Ingo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 26, 2007, 9:48am UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/4 "2007-03-26T09:48:47Z")

</div>

Thanks again Eden,

this was helpful for me. I didn’t get the double backslash (escape)  
point yesterday.

Regards  
Ingo

> Where is your RJS being loaded up?
> 
> Your escaping is a off with the newlines, so that could be the issue.  
> When you use #\<\<, you’ll be dumping a string directly to javascript,  
> and ruby’s quoting is getting in the way, so “$(‘article\_body’).value  
> += ‘\n\blah\n’” would be sent to your browser as:
> 
> "$(‘article\_body’).value += ’  
> \blah  
> ’
> 
> Instead you should escape the newlines. Also, you’ll need to double  
> escape the \ before the b, because ‘\b’ will get interpreted in  
> javascript as a backspace character: “$(‘article\_body’).value += ‘\n\  
> \blah\n’”. Or you can use ruby single quotes and heredocs to make  
> things look nicer although you won’t be able to insert variables into  
> the string block.
> 
> page \<\< \<\<-‘end’  
> $(‘article\_body’).value += ‘\n\blah\n’;  
> end
> 
> On Mar 26, 5:23 am, Ingo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 26, 2007, 4:17am UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/5 "2007-03-26T04:17:42Z")

</div>

Where is your RJS being loaded up?

Your escaping is a off with the newlines, so that could be the issue.  
When you use #\<\<, you’ll be dumping a string directly to javascript,  
and ruby’s quoting is getting in the way, so “$(‘article\_body’).value  
+= ‘\n\blah\n’” would be sent to your browser as:

"$(‘article\_body’).value += ’  
\blah  
’

Instead you should escape the newlines. Also, you’ll need to double  
escape the \ before the b, because ‘\b’ will get interpreted in  
javascript as a backspace character: “$(‘article\_body’).value += ‘\n\  
\blah\n’”. Or you can use ruby single quotes and heredocs to make  
things look nicer although you won’t be able to insert variables into  
the string block.

page \<\< \<\<-‘end’  
$(‘article\_body’).value += ‘\n\blah\n’;  
end

On Mar 26, 5:23 am, Ingo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dite](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dite](https://www.ruby-forum.com/u/dite)\
**Post date:** [March 19, 2009, 11:46am UTC](https://www.ruby-forum.com/t/insert-text-into-text-area-field-with-rjs/90072/6 "2009-03-19T11:46:57Z")

</div>

Ingo P. wrote:

> Thanks again Eden,
> 
> this was helpful for me. I didn’t get the double backslash (escape)  
> point yesterday.
> 
> Regards  
> Ingo
> 
> > Where is your RJS being loaded up?
> > 
> > Your escaping is a off with the newlines, so that could be the issue.  
> > When you use #\<\<, you’ll be dumping a string directly to javascript,  
> > and ruby’s quoting is getting in the way, so “$(‘article\_body’).value  
> > += ‘\n\blah\n’” would be sent to your browser as:
> > 
> > "$(‘article\_body’).value += ’  
> > \blah  
> > ’
> > 
> > Instead you should escape the newlines. Also, you’ll need to double  
> > escape the \ before the b, because ‘\b’ will get interpreted in  
> > javascript as a backspace character: “$(‘article\_body’).value += ‘\n\  
> > \blah\n’”. Or you can use ruby single quotes and heredocs to make  
> > things look nicer although you won’t be able to insert variables into  
> > the string block.
> > 
> > page \<\< \<\<-‘end’  
> > $(‘article\_body’).value += ‘\n\blah\n’;  
> > end
> > 
> > On Mar 26, 5:23 am, Ingo P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi,

This helps for me but I have 1 doubt

def add\_text\_link(name)  
link\_to\_function name do |page|  
begin  
page \<\< \<\<-‘end’  
$(‘text\_area’).value += “\n Hello world” ;  
end  
end

I added above code in the RJS, this works but if I need to use the name  
parameter that is passed in how do I do it?

I tried #{name} but no luck…

Thanks,  
Sudhindra
