Ajax replace div


#1

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


#2

maybe you could set it to be hidden or display:none.

I dont know your context, so that might not work visually for your.


#3

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 %>

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.html#Reference
http://www.codyfauser.com/pages/rjs-plugin

Chris