# Observe\_field and on:focus

**URL:** <https://www.ruby-forum.com/t/observe-field-and-on-focus/127776>\
**Category:** Rails\
**Created:** [February 1, 2008, 7:16am UTC](https://www.ruby-forum.com/t/observe-field-and-on-focus/127776 "2008-02-01T07:16:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 1, 2008, 7:16am UTC](https://www.ruby-forum.com/t/observe-field-and-on-focus/127776/1 "2008-02-01T07:16:29Z")

</div>

Hi,

I want to give suggestions(basically showing a hidden div) when somebody  
clicks on the text box to write(I assumed this is what on:focus meant).

\<%= observe\_field :test\_field, :function =\> “alert(‘Element on focus’)”,  
:on =\> :focus %\>

But surpisingly, alert is called when the value is changed and focus  
moves onto something else…

Can somebody suggest? how to get this working?

Regards,  
Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 1, 2008, 8:30am UTC](https://www.ruby-forum.com/t/observe-field-and-on-focus/127776/2 "2008-02-01T08:30:36Z")

</div>

\<%= text\_area ‘test’, ‘field’, “cols” =\> 60, “rows” =\> 4 , :onclick =\>  
“$(‘tips’).show();”, :onBlur =\> “$(‘tips’).hide();” %\>

This did the trick; 🙂

Sandeep G. wrote:

> Hi,
> 
> I want to give suggestions(basically showing a hidden div) when somebody  
> clicks on the text box to write(I assumed this is what on:focus meant).
> 
> \<%= observe\_field :test\_field, :function =\> “alert(‘Element on focus’)”,  
> :on =\> :focus %\>
> 
> But surpisingly, alert is called when the value is changed and focus  
> moves onto something else…
> 
> Can somebody suggest? how to get this working?
> 
> Regards,  
> Sandeep G

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 1, 2008, 8:55am UTC](https://www.ruby-forum.com/t/observe-field-and-on-focus/127776/3 "2008-02-01T08:55:34Z")

</div>

> > \<%= observe\_field :test\_field, :function =\> “alert(‘Element on focus’)”,  
> > :on =\> :focus %\>

observe\_field is used to check whether any changes made to the  
particular field…  
Once u did any change in this particular field that alert was called.

---

<div class="post-metadata">

**Author:** ![sgudibanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@sgudibanda](https://www.ruby-forum.com/u/sgudibanda)\
**Post date:** [February 1, 2008, 9:00am UTC](https://www.ruby-forum.com/t/observe-field-and-on-focus/127776/4 "2008-02-01T09:00:47Z")

</div>

Ratnavel S. wrote:

> > > \<%= observe\_field :test\_field, :function =\> “alert(‘Element on focus’)”,  
> > > :on =\> :focus %\>
> 
> observe\_field is used to check whether any changes made to the  
> particular field…  
> Once u did any change in this particular field that alert was called.

I understand the on: options it to have control over the events like  
focus, click, change, blur and such…  
It is just that the code in prototype is forcing the default change  
irrelevant to :on option that we choose.
