# Ajax response event ? (Should be easy..)

**URL:** <https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894>\
**Category:** Rails\
**Created:** [March 24, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894 "2006-03-24T22:13:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [March 24, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894/1 "2006-03-24T22:13:53Z")

</div>

How do you call a javascript function to do something when the response  
is returned to the visitors browser?

Easy ey?

//Daniel

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [March 24, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894/2 "2006-03-24T23:08:36Z")

</div>

Many of the built-in AJAX actions in Rails such as the link\_to\_remote  
allow you to specify JavaScript to execute upon completion by setting  
the :success attribute. In this example, I show the spinner div while  
the processing occurs, and hide it when it is complete.

\<%= link\_to\_remote( “#{state.title}”,  
:update =\> “state\_editor”,  
:before =\> %(Element.show(‘spinner’)),  
:success =\> %(Element.hide(‘spinner’)) ,  
:url =\>{ :action =\> “load\_state”, :id =\> state.id }) %\>

Hope this helps ~

Ben

On 3/24/06, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Ben R.  
[http://www.benr75.com](http://www.benr75.com)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [March 27, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894/3 "2006-03-27T16:39:36Z")

</div>

Thanks, thats what Im looking for.

Could someone please elaborate on how to override the setting of  
success/failure attribute?

Thanks in advance

// Daniel

Ben R. wrote:

> Many of the built-in AJAX actions in Rails such as the link\_to\_remote  
> allow you to specify JavaScript to execute upon completion by setting  
> the :success attribute. In this example, I show the spinner div while  
> the processing occurs, and hide it when it is complete.
> 
> \<%= link\_to\_remote( “#{state.title}”,  
> :update =\> “state\_editor”,  
> :before =\> %(Element.show(‘spinner’)),  
> :success =\> %(Element.hide(‘spinner’)) ,  
> :url =\>{ :action =\> “load\_state”, :id =\> state.id }) %\>
> 
> Hope this helps ~
> 
> Ben
> 
> On 3/24/06, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Ben R.  
> [http://www.benr75.com](http://www.benr75.com)

---

<div class="post-metadata">

**Author:** ![daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ec8ea/32.png) [@daniel](https://www.ruby-forum.com/u/daniel)\
**Post date:** [March 27, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/ajax-response-event-should-be-easy/86894/4 "2006-03-27T17:13:09Z")

</div>

More info on what Im trying to do:

I submit a login form using ajax.  
On the server side I check if the submitted info is valid.  
Depending on if the submitted info is valid or not I send back different  
partials.

If the info was invalid I would like to set the failure attribute so  
that I can pop up a javascript alert in the browser.

Is this doable?

//Daniel
