Forum: Ruby on Rails Whats the best Ajax way to update a textarea's text?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrew C. (Guest)
on 2006-04-14 20:06
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
Roberto S. (Guest)
on 2006-04-14 20:46
(Received via mailing list)
just set the value of the textarea element with a  RJS
Andrew C. (Guest)
on 2006-04-14 20:47
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. :)

-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
Michael T. (Guest)
on 2006-04-14 20:58
(Received via mailing list)
I was looking for this same solution just the other day, so thank you.

Michael
Michael T. (Guest)
on 2006-04-14 22:06
(Received via mailing list)
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
Andrew C. (Guest)
on 2006-04-14 22:44
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
Michael T. (Guest)
on 2006-04-14 23:08
(Received via mailing list)
Interesting.  Thought I tried it both ways, just tried again and it
works.  Thanks.

Michael
Bill W. (Guest)
on 2006-04-20 18:52
(Received via mailing list)
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 <div> with id=Entryline.  When the user clicks the button, a
new
line is added to the page inside a <div> 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
<div>
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?
Cody F. (Guest)
on 2006-04-21 01:06
(Received via mailing list)
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. :)
> >> Thanks,
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Cody F.
http://www.codyfauser.com
Cody F. (Guest)
on 2006-04-21 01:15
(Received via mailing list)
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
Bill W. (Guest)
on 2006-04-21 01:25
(Received via mailing list)
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
Michael T. (Guest)
on 2006-04-21 02:11
(Received via mailing list)
Thanks Cody.  That makes sense.

Michael
This topic is locked and can not be replied to.