Topic: Focusing cursor unobtrusively?

I have a method that focuses the cursor in various inputs depending on the page:

def focus(domid)
  raw("<script>$(document).ready(function () { $('##{domid}').focus(); });</script>")

So if you had a Person model with a Name attribute, on the form for the person you would specify:

<%= focus "person_name" %>

And it works fine.  However, this is raw JS output to the HTML file.  Is there an alternative to having this same functionality but doing it unobtrusively?

Re: Focusing cursor unobtrusively?

The obvious method is to directly assign a special class (say 'focus-on-me') to the field in question and then unobtrusively find via this class.

However, seeing your experience, I suspect there must be more to it; perhaps it is impractical or impossible to inject the class name at the field generation.  If that's this case I suggest you wrap the whole form in a block (via the block_helpers gem; see: or roll your own). Then either re-write the field in question before releasing it to the Rails view generation code (i.e. in the block helper's display method); or -- perhaps even better -- place the id as an attribute into a wrapping div (pass the id in to the block helper and do the wrapping with the block helper's display method).

From there on it's simple document.ready javascript: extract id/class, locate field, focus.

I may be totally missing your point, though.