Whats the best Ajax way to update a textarea's text?


#1

I am looking for a way to update the text-value within a textarea from
an Ajax render call inside of a controller. Is there a way to do this
without rendering an the partial containing the textarea?

Thanks,
Andy


#2

just set the value of the textarea element with a RJS


#3

Disregard, I believe I found a working solution with the following:

render :update do |page|
page[‘messages’].value = @messages
end

where ‘messages’ is the id of the textarea and @messages is updated in
the controller with the current state of the text.

Looks to be working, if anyone sees a reason why this may be a problem
and has a better solution I’d love to hear it. :slight_smile:

-Andy

Andrew C. wrote:

I am looking for a way to update the text-value within a textarea from
an Ajax render call inside of a controller. Is there a way to do this
without rendering an the partial containing the textarea?

Thanks,
Andy


#4

I was looking for this same solution just the other day, so thank you.

Michael


#5

This does work for me with text_fields also, although I reference the
element by

page[‘element_id’].value instead of page.element_id.value

-Andy

Michael T. wrote:

This doesn’t appear to work with a text_field_tag unless I’m missing
something. Have:

page.email.value = ‘’

to clear the text field. email is the ID of my input control. Any
ideas?

Michael


#6

Interesting. Thought I tried it both ways, just tried again and it
works. Thanks.

Michael


#7

This doesn’t appear to work with a text_field_tag unless I’m missing
something. Have:

page.email.value = ‘’

to clear the text field. email is the ID of my input control. Any
ideas?

Michael


#8

Hi Andy,

I’m trying to clear the input fields on a form and was really glad to
find
this thread. Unfortunately, I’ve tried this just about every way I can
think of and instead of clearing the fields, it’s clearing pretty much
the
whole page. I would really appreciate some help!

The page has a form that allows the entry of two fields. The form
exists
inside a

with id=Entryline. When the user clicks the button, a
new
line is added to the page inside a
with id=ingredient_list. The
user
can add as many lines as they want. I want to clear the fields each
time
they click the button. What’s happening instead is that the Entryline
and everything above it on the page is getting wiped out. The code is below. Any ideas?

Thanks in advance,
Bill

— Here’s _entry_form.rhtml —
<%= form_remote_tag(:update => ‘ingredient_list’,
:url => {:action => :add_ingredient},
:position => ‘bottom’,
:after => {:action => :clear_entry}) %>
Name:
<%= text_field_tag :name %>
Amount:
<%= text_field_tag :qty %>
<%= submit_tag ‘Add Ingredient’ %>
<%= end_form_tag %>

— and here’s the clear_entry action in the controller ----
def clear_entry
render :update do |page|
page.Entryline.name.value = ‘’
page.Entryline.qty.value = ‘’
end
end
----- Original Message -----
From: “Andrew C.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Friday, April 14, 2006 11:47 AM
Subject: [Rails] Re: Whats the best Ajax way to update a textarea’s
text?


#9

Michael,

You were trying to use a class proxy instead of an element proxy.
Compare the JavaScript generated by the following:

Class proxy

page.email.value = ‘’ # => Email.value = “”;

Element proxy

page[‘email’].value = ‘’ # => $(“email”).value = “”;

On 4/14/06, Michael T. removed_email_address@domain.invalid wrote:

removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com


#10

Bill,

The :after callback doesn’t make a remote Ajax call and doesn’t really
need to. Try this to reset the fields:

<%= form_remote_tag(:update => ‘ingredient_list’,
:url => {:action => :add_ingredient},
:position => ‘bottom’,
:after => ‘Form.reset(this)’
) %>

This will just call the Prototype form helper to reset your form. If
you are using RJS in your add_ingredient action then you could just do
the resetting from your RJS template.

Just give the form an id with :html => { :id => ‘entry-form’ } and
then in RJS you could use a JavaScript class proxy like follows:

page.form.reset ‘entry-form’

Also, if your controller action ‘add_ingredient’ renders an RJS
template or uses inline RJS then you don’t want to use the :update
option in your link_to_remote method call. This could be causing your
strange problems.

On 4/20/06, Bill W. removed_email_address@domain.invalid wrote:

can add as many lines as they want. I want to clear the fields each time
:position => ‘bottom’,
render :update do |page|

and has a better solution I’d love to hear it. :slight_smile:

Thanks,


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com


#11

Thanks Cody. That makes sense.

Michael


#12

Cody,

Thank you! Perfect.

Best regards,
Bill

----- Original Message -----
From: “Cody F.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: Thursday, April 20, 2006 4:05 PM
Subject: Re: [Rails] Re: Whats the best Ajax way to update a textarea’s
text?

Bill,

The :after callback doesn’t make a remote Ajax call and doesn’t really
need to. Try this to reset the fields:

<%= form_remote_tag(:update => ‘ingredient_list’,
:url => {:action => :add_ingredient},
:position => ‘bottom’,
:after => ‘Form.reset(this)’
) %>

This will just call the Prototype form helper to reset your form. If
you are using RJS in your add_ingredient action then you could just do
the resetting from your RJS template.

Just give the form an id with :html => { :id => ‘entry-form’ } and
then in RJS you could use a JavaScript class proxy like follows:

page.form.reset ‘entry-form’

Also, if your controller action ‘add_ingredient’ renders an RJS
template or uses inline RJS then you don’t want to use the :update
option in your link_to_remote method call. This could be causing your
strange problems.

On 4/20/06, Bill W. removed_email_address@domain.invalid wrote:

user
<%= form_remote_tag(:update => ‘ingredient_list’,
— and here’s the clear_entry action in the controller ----
Subject: [Rails] Re: Whats the best Ajax way to update a textarea’s text?

without rendering an the partial containing the textarea?
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails