# Delete div content automatically after 2 seconds

**URL:** <https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391>\
**Category:** Rails\
**Created:** [April 19, 2007, 12:47am UTC](https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391 "2007-04-19T00:47:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 19, 2007, 12:47am UTC](https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391/1 "2007-04-19T00:47:21Z")

</div>

Hi everyone,  
I’d like to display a text in a div (an error message returned from an  
ajax  
form) for 2 seconds and then delete the div content.  
I do not want the div itself to fade out using scriptacolous - I want  
the content itself overwritten with an empty string.

Is there a way of doing that in rails?

Thanks!

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 19, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391/2 "2007-04-19T14:40:34Z")

</div>

The best way of doing this is in Javascript. Scriptaculous is purely an  
effects framework and has in and of itself nothing to do with Rails. If  
you  
want to fade out/slide out, etc, then you can add such an effect later.

For just deleting the error div after 2 seconds, you’ll need to use this  
Javascript (using Prorotype):

setTimeout(2000, deleteDiv(‘div\_id’));

function deleteDiv(div) {  
// Effect call can go here  
Element.remove(div);  
}

Where this goes of course depends on how the error div is displayed. If  
this  
is a full page refresh, then a simple tag after the div will  
work.

Jason

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 19, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391/3 "2007-04-19T15:19:22Z")

</div>

Ehud R. wrote:

> Hi everyone,  
> I’d like to display a text in a div (an error message returned from an  
> ajax  
> form) for 2 seconds and then delete the div content.  
> I do not want the div itself to fade out using scriptacolous - I want  
> the content itself overwritten with an empty string.
> 
> Is there a way of doing that in rails?
> 
> Thanks!

RJS can contain raw JavaScript, so I’d make the result of the Ajax call  
a bit of RJS rather than HTML. The RJS can set the div contents to  
message text, then add a timeout function as above to replac it in 2s  
with “”.

A.

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 19, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/delete-div-content-automatically-after-2-seconds/92391/4 "2007-04-19T17:19:15Z")

</div>

You can also achieve this with RJS / Rails Helpers:

if the error message is set thorugh an AJAX response, you would do  
something like in the rjs partial you render for the response:

page.replace\_html :div\_id “Your erro message here”  
page.delay(2) do  
page.replace\_html :div\_id, “”  
end

if it’s a complete page refresh:

\<%= @error\_msg

On 19 Apr., 15:19, Alan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
