# observe\_field, does this generate HTML?

**URL:** <https://discuss.rubyonrails.org/t/observe-field-does-this-generate-html/15660>\
**Category:** rubyonrails-talk\
**Created:** [July 14, 2007, 9:49pm UTC](https://discuss.rubyonrails.org/t/observe-field-does-this-generate-html/15660 "2007-07-14T21:49:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Phlip](https://discuss.rubyonrails.org/u/Phlip)\
**Post date:** [July 14, 2007, 9:49pm UTC](https://discuss.rubyonrails.org/t/observe-field-does-this-generate-html/15660/1 "2007-07-14T21:49:10Z")

</div>

Michael Chow wrote:

> I don't have Rails installed at the moment, I'd like to check myself, but it's just a simple curiosity.

Install Rails, Firefox, & Firebug, and try it.

> observe\_field fits into replacement tags \<%= %\>, which caught my attention because as far as I know it's just javascript observing a field and should not stick anything into HTML. Is \<%= merely used as an enabling method, or will there actually be some sort of HTML object there?

When I use a \<%= form\_remote\_for ... %\>, and when I run my site in Firefox, point my mouse to the form, and get the "Context Menu" (the "right" mouse button), Firebug has a feature "inspect element". This shows the form in a panel of my browser, like this:

\<form onsubmit="new Ajax.Request('/ring/set\_attack\_mode', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post" id="attack\_form" action="/ring/set\_attack\_mode"\>

The \<%%\> part turned into a big splatch of JavaScript, calling the Prototype.js (or Scriptaculous?) method Ajax.Request. And that hides all the ugly magic required to send the ajax commands to my controller 'ring', action 'set\_attack\_mode'.

The observe\_field stuff must work the same general way - a Rails method, in Ruby, wrapping a Scriptaculous method, in JavaScript.

> Also, does the observer have to come after the field it is observing or can I stick them all at the top of the HTML?

Put them near their observers, so they are clear to read.

---

<div class="post-metadata">

**Author:** ![mboeh](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@mboeh](https://discuss.rubyonrails.org/u/mboeh)\
**Post date:** [July 18, 2007, 4:21pm UTC](https://discuss.rubyonrails.org/t/observe-field-does-this-generate-html/15660/2 "2007-07-18T16:21:25Z")

</div>

observe\_field and observe\_form both generate SCRIPT elements containing that Javascript.
