# Reset comment form using js or RJS

**URL:** <https://www.ruby-forum.com/t/reset-comment-form-using-js-or-rjs/145135>\
**Category:** Rails\
**Created:** [August 6, 2008, 11:08am UTC](https://www.ruby-forum.com/t/reset-comment-form-using-js-or-rjs/145135 "2008-08-06T11:08:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 11:08am UTC](https://www.ruby-forum.com/t/reset-comment-form-using-js-or-rjs/145135/1 "2008-08-06T11:08:15Z")

</div>

I have a placeholder ‘pre-text’ value which is given to a comment form  
input via the application.js;

Event.observe(window, ‘load’,

comment\_pre\_text = “Leave a comment…”  
var el = $(‘comment\_body’)  
if(el) {  
el.value = comment\_pre\_text  
Event.observe(el, ‘focus’, function() { if(this.value ==  
comment\_pre\_text) this.value = ‘’; })  
Event.observe(el, ‘blur’, function() { if(this.value == ‘’) this.value =  
comment\_pre\_text; })  
}

}  
);

However, I’ve since started using RJS to make submissions through the  
comment form, and now the pre-text js breaks - it doesn’t show the  
pre-text after a form submission unless I click in and then out of the  
comment form. This problem could be fixed by turning the comments in the  
following RJS template in to working code (which I haven’t worked out  
how to do yet);

page.insert\_html :bottom, :items\_wrapper, :partial =\> “comment”, :object  
=\> @comment  
page[:comment\_form].reset

# focus on the comment input

# focus out of the comment input

page.visual\_effect :highlight, “comment\_#{@comment.id}”  
flash.discard

Alternatively, I expect this problem could also be fixed by adding  
another line to the application.js, which I imagine would read something  
like this (I’m new to js and I can’t find a ‘reset’ equivalent to  
‘focus’ or ‘blur’ yet - both of which I don’t understand fully);

Event.observe(el, ‘reset’, function() { if(this.value == ‘’) this.value  
= comment\_pre\_text; })  
}

Any ideas?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 12:04pm UTC](https://www.ruby-forum.com/t/reset-comment-form-using-js-or-rjs/145135/2 "2008-08-06T12:04:52Z")

</div>

Neil ,

> I can’t find a ‘reset’ equivalent …

Prototype has a form reset method :  
Form.reset(‘my\_form’)  
or  
$(my\_form’).reset()

api doc: [Prototype API Documentation | Form.reset (Deprecated URL)](http://www.prototypejs.org/api/form/reset)

Alain

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 6, 2008, 12:24pm UTC](https://www.ruby-forum.com/t/reset-comment-form-using-js-or-rjs/145135/3 "2008-08-06T12:24:18Z")

</div>

Alain R. wrote:

> Neil ,
> 
> > I can’t find a ‘reset’ equivalent …
> 
> Prototype has a form reset method :  
> Form.reset(‘my\_form’)  
> or  
> $(my\_form’).reset()
> 
> api doc: [Prototype API Documentation | Form.reset (Deprecated URL)](http://www.prototypejs.org/api/form/reset)
> 
> Alain

Thanks Alain. I’ve tried taking the approach that I thought would work,  
i.e. telling the application.js to watch for when the comment\_form is  
reset (by the RJS) and to do the pre-text when the event occurs;

comment\_pre\_text = “Leave a comment…”  
var el = $(‘comment\_body’)  
if(el) {  
el.value = comment\_pre\_text  
Event.observe(el, ‘focus’, function() { if(this.value ==  
comment\_pre\_text) this.value = ‘’; })  
Event.observe(el, ‘blur’, function() { if(this.value == ‘’) this.value =  
comment\_pre\_text; })  
Event.observe(el.reset(), function() { if(this.value == ‘’) this.value =  
status\_pre\_text; })  
}

That approach doesn’t seem to work. Do you have any ideas on that one?  
Or do you know if there’s an RJS helper for focusing in and out of an  
input?
