Insert text into text area field with RJS

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

When in doubt, drop to javascript:

page << “$(‘article_body’).value += ‘blah’”

On Mar 26, 2:10 am, Ingo P. [email protected]

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 += 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. [email protected]

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. [email protected]

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. [email protected]

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. [email protected]

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