# Ajax replace div

**URL:** <https://www.ruby-forum.com/t/ajax-replace-div/55755>\
**Category:** Rails\
**Created:** [March 23, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/ajax-replace-div/55755 "2006-03-23T16:49:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastien\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Sebastien\_H](https://www.ruby-forum.com/u/Sebastien_H)\
**Post date:** [March 23, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/ajax-replace-div/55755/1 "2006-03-23T16:49:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sebastien\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Sebastien\_H](https://www.ruby-forum.com/u/Sebastien_H)\
**Post date:** [March 23, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/ajax-replace-div/55755/2 "2006-03-23T17:08:33Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Sebastien\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Sebastien\_H](https://www.ruby-forum.com/u/Sebastien_H)\
**Post date:** [March 23, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/ajax-replace-div/55755/3 "2006-03-23T18:02:27Z")

</div>

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:

> **[Using prototype.js v1.5.0](https://www.sergiopereira.com/articles/prototype.js.html#Reference)**
>
> Prototype.js documentation

[http://www.codyfauser.com/pages/rjs-plugin](http://www.codyfauser.com/pages/rjs-plugin)

Chris
