Forum: Ruby on Rails ajax replace div

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.
8987dd66d952d35845f19ae8f6d78291?d=identicon&s=25 Sebastien Hugues (Guest)
on 2006-03-23 16:49
(Received via mailing list)
Hi,

Using form_remote_tag methode, is there a clean way to
directly replace the content of a given div instead of just update it ?

If I destroy the content of the div using this code:

:success => '$(\'mydiv').innerHTML = \'\'',

It works. But it's not really a clean way...

Any suggestions are very welcome..
Thanks.

Seb
366472c969c9754ae2b8079e42fed1ca?d=identicon&s=25 Jon Maddox (Guest)
on 2006-03-23 17:08
(Received via mailing list)
maybe you could set it to be hidden  or display:none.

I dont know your context, so that might not work visually for your.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-23 18:02
(Received via mailing list)
first, there is nothing wrong with the way you are doing it.

other options:

// replace/update the contents of mydiv
Element.update('mydiv', 'new content')

// hide (set display style to 'none')
Element.hide('mydiv')

// removes element from document
Element.remove('mydiv')

example

<%= form_remote_tag :update => "update_div", :url => { :action =>
"do_something" }, :success => "Element.update('some_other_div', '')" %>
<%= end_form_tag %>
<div id="update_div"></div>
<div id="some_other_div"></div>

so what the above does is 2 things:

its updates the "update_div" with the results of the ajax call to the
do_something action, then, upon success, replaces (updates) the
"some_other_div" with *blank*

you could accomplish much the same thing with rjs, which to me, is
cleaner.

you would remove the :update and :success from the form_remote_tag then
create an rjs template for your action (do_something.rjs) with the
contents
of:

page.replace_html("update_div", "content") # or you could do :partial =>
"partial_name" in place of content if you have a partial you'd like to
use
page.replace_html("some_other_div", "")

and that's it.

references:

http://www.sergiopereira.com/articles/prototype.js...
http://www.codyfauser.com/pages/rjs-plugin


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