Topic: How to make textarea focus in Rails?

Hi!

How to focus on textarea using helper form_tag? Or how is the best way to make focus in Rails?

Right now it opens new form (using ajax) but new form doesn't have focus. So user have to click in order to input text.

view\cards\new.rb

     <%= link_to_remote 'Add a new', :update => 'add_a_new',
                                     :url => {:action => 'add_a_new'},
                                     :complete => 'document.chapter.name.focus();' %>


How to put form name using helper? I mean javascript would need this name (correct if i'm wrong)... document.formname.name.focus();


viwes\cards\_add_a_new.rhtml

  <%= form_tag (:action => 'create_remote_chapter', :kohalik => 'kohalik') %>
    <%= text_field :chapter, :name, :size => 15 %>
    <%= submit_tag "Submit" %>
  <%= end_form_tag %>


Thanks if anybody can help me out!
Priit

Estonian ruby coders => http://ruby.ee

Re: How to make textarea focus in Rails?

Prototype includes an excellent helper for finding some element on a page.  It's a function that is named with a dollar-sign symbol.  The only restriction is you have to have an "id" in the element you're looking for.  So you'll want to add an id to your form and make it like so:

     <%= link_to_remote 'Add a new', :update => 'add_a_new',
                                     :url => {:action => 'add_a_new'},
                                     :complete => '$('my_form').focus();' %>

  <%= form_tag ({:action => 'create_remote_chapter', :kohalik => 'kohalik'}, :html => 'my_form') %>
    <%= text_field :chapter, :name, :size => 15 %>
    <%= submit_tag "Submit" %>
  <%= end_form_tag %>

Re: How to make textarea focus in Rails?

Thank you, danger!

I'll post my nuby mistakes (sometimes it might be useful for others)


1) First thing.
       A little wrong way (not using {})

            <%= form_tag(:action => 'create', :name => 'myform') %>

            it gives:
            <form action="/cards/create?name=myform" method="post">

       Correct way (using {})
           
             <%= form_tag({:action => 'create'}, :name => 'myform') %>

             it gives:
             <form action="/cards/create" method="post" name="myform">

       Nuby question: Why do you use :html not :name? (just curious)



2) Second thing.

       Actually focus works without :html (or :name). Thanks for pointing out DollarFunction $ (more about it at http://wiki.script.aculo.us/scriptaculo … arFunction ), it helped a lot (because when you just use document.chapter_name.focus(); then firefox works but IE6 and IE7 does not. When you use DollarFunction it works with all main browsers.

My working code:

view\cards\new.rb
       <%= link_to_remote 'Add a new', :update => 'add_a_new',
                                     :url => {:action => 'add_a_new'},
                                     :complete => '$("chapter_name").focus();' %>

viwes\cards\_add_a_new.rhtml
       <%= form_tag (:action => 'create_remote_chapter', :kohalik => 'kohalik') %> 
               <%= text_field :chapter, :name, :size => 15 %> 
               <%= submit_tag "Submit" %> 
       <%= end_form_tag %>


Thanks for help!
Priit

Estonian ruby coders => http://ruby.ee

Re: How to make textarea focus in Rails?

Awesome dude.  Yeah, looking back I see I had several errors in my post - what the hell was I doing with a :html key?  Way to figure it out though and thanks for posting the final solution!