# AJAX Observe Form - problem with event handlers with :on

**URL:** <https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684>\
**Category:** Rails\
**Created:** [September 14, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684 "2006-09-14T15:27:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 3:27pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/1 "2006-09-14T15:27:19Z")

</div>

I have the following:

\<%= observe\_form “picker\_form”,:frequency =\> 1,  
:update =\> “frame\_picker” ,  
:loading\_text =\> “Retrieving price…”,  
:complete =\> visual\_effect(:highlight,  
“picker\_live\_price”, { :startcolor =\> “’#9b3801’”, :endcolor =\>  
“’#f0c18e’”, :duration =\> 0.5 }),  
:url =\> { :controller =\> ‘store’,  
:action =\> “update\_picker” } %\>

It works as expected, the result is that a form is redrawn to  
dynamically populate drop-down lists. There is one problem - the AJAX  
update triggers if the user just hovers over a new value in a drop-down.  
The drop-down then disappears as the form is redrawn.

I want the AJAX to fire only when the user clicks on an option, not for  
a hover. I’ve tried adding :on =\> “blur” to the observe\_form call but it  
doesn’t seem to make a difference.

I appreciate any help. I’m redrawing the entire form on the AJAX call -  
my next step is to change to redrawing individual select dropdowns, but  
that is a lot more code and it would appear from the api documentation  
that if I can find the right event for the :on =\> ? that I can get it  
working the way I want.

thanks.  
c.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/2 "2006-09-14T18:15:16Z")

</div>

Remove :frequency statement.  
In that case that will be event driven.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/3 "2006-09-14T19:20:26Z")

</div>

Aw crud on a stick - can it really be that simple? Could it really have  
been right in front of me as I stared at the api docs?

:frequency: The frequency (in seconds) at which changes to this field  
will be detected. Not setting this option at all or to a value equal to  
or less than zero will use event based observation instead of time based  
observation.

Yes. Yes.

Thanks so much Sergey. 🙂

c.

Sergey K. wrote:

> Remove :frequency statement.  
> In that case that will be event driven.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/4 "2006-09-14T20:19:07Z")

</div>

I also see that the RJS templates seem to trump the method I’m using in  
a major way. I just grabbed the O’Reilly RJS Templates for Rails booklet  
and it looks to be a gold mine.

c.

Sergey K. wrote:

> Cayce B. wrote:
> 
> > Aw crud on a stick - can it really be that simple? Could it really have  
> > been right in front of me as I stared at the api docs?
> > 
> > :frequency: The frequency (in seconds) at which changes to this field  
> > will be detected. Not setting this option at all or to a value equal to  
> > or less than zero will use event based observation instead of time based  
> > observation.
> 
> I was hit the same brick time ago =) As an old geek I always like to see  
> the actual code that’s how I realized  
> about it. Even if that was on front of me in documentation =)
> 
> Please note that it won’t work with radio buttons. Stop! Probably it may  
> if you will provide id name only on the last  
> element! In that case you can safely update any  
> DOM elements  
> via observe\_(field|form ) with :complete field and I don’t have to use  
> remote\_function which is doesn’t work properly  
> with IE on radiobuttons. Cool!  
> I have to check it.
> 
> > Yes. Yes.
> > 
> > Thanks so much Sergey. 🙂
> 
> You are very welcome!
> 
> > 
> 
> All the Best!  
> Sergey.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 8:59pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/5 "2006-09-14T20:59:28Z")

</div>

Cayce B. wrote:

> I also see that the RJS templates seem to trump the method I’m using in  
> a major way. I just grabbed the O’Reilly RJS Templates for Rails booklet  
> and it looks to be a gold mine.
> 
> c.

It looks like I will get my copy of that book soon =)))

> > > or less than zero will use event based observation instead of time based  
> > > element! In that case you can safely update any

All the Best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [September 14, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/6 "2006-09-14T19:53:09Z")

</div>

Cayce B. wrote:

> Aw crud on a stick - can it really be that simple? Could it really have  
> been right in front of me as I stared at the api docs?
> 
> :frequency: The frequency (in seconds) at which changes to this field  
> will be detected. Not setting this option at all or to a value equal to  
> or less than zero will use event based observation instead of time based  
> observation.

I was hit the same brick time ago =) As an old geek I always like to see  
the actual code that’s how I realized  
about it. Even if that was on front of me in documentation =)

Please note that it won’t work with radio buttons. Stop! Probably it may  
if you will provide id name only on the last  
 element! In that case you can safely update any  
DOM elements  
via observe\_(field|form ) with :complete field and I don’t have to use  
remote\_function which is doesn’t work properly  
with IE on radiobuttons. Cool!  
I have to check it.

> Yes. Yes.
> 
> Thanks so much Sergey. 🙂

You are very welcome!

> 

All the Best!  
Sergey.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 22, 2007, 5:20am UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/7 "2007-06-22T05:20:10Z")

</div>

Hello, I have a similar proble with the :on property  
I have the following code in the view

\<%= start\_form\_tag :id =\> ‘AjaxForm’ %\>  
\<%= text\_field\_tag :text1 %\>  
\<%= text\_field\_tag :text2 %\>  
\<%= observe\_field :text1,  
:url =\> {:action =\> ‘test’},  
:update =\> ‘ajaxWrapper’,  
:with =\> ‘text1’,  
:on =\> ‘blur’ %\>  
\<%= end\_form\_tag %\>

The method ‘test’ is  
def test  
render :text =\> ‘hello world’  
end

The thing is that the observer doesn’t trigger on blur and it does  
trigger onchange (which is the default) even if I set it to :on =\>  
‘blur’.  
The same happens with ‘click’, ‘focus’, and any other event that I try.

If someone knows something about it , please let me know.

Thanks

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [June 22, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/ajax-observe-form-problem-with-event-handlers-with-on/68684/8 "2007-06-22T13:30:16Z")

</div>

On Jun 21, 2007, at 11:20 PM, Fernando P. wrote:

> ```
> :on => 'blur' %>
> 
> ```
> 
> trigger onchange (which is the default) even if I set it to :on =\>  
> ‘blur’.  
> The same happens with ‘click’, ‘focus’, and any other event that I  
> try.
> 
> If someone knows something about it , please let me know.
> 
> Thanks

If you dive down into the source code, you find that the only :on  
value that has support at the JavaScript level is ‘change’ and that  
it is very specific. A few months ago I had to track down some  
unexpected behavior (or more accurately the lack of expected  
behavior) and discovered this. That was with Rails 1.1.6, but I  
think it’s true at least as recently as 1.2.2

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
