This is probably a general HTML question, but I''ll ask it here anyway. Is there any way to set the focus to a specific field in a form generated in a view? The little I''ve found on so far suggests that I need to add some javascript to do this. I can go figure out how to do that, but it seems like it should be the sort of thing that could already be built into Rails. --wpd --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Müller
2008-Dec-05 09:50 UTC
Re: How to set the focus to a specified field in a form
this one requires prototype (for textfield with id "searchtext"): <script language="JavaScript"> $("searchtext").focus(); </script> without prototype it would be something like this: document.formname.fieldname.focus(); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Bartlett
2008-Dec-05 11:27 UTC
Re: How to set the focus to a specified field in a form
On Dec 5, 10:50 pm, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> this one requires prototype (for textfield with id "searchtext"): > > <script language="JavaScript"> > $("searchtext").focus(); > </script>Does that work for you in Internet Explorer? It didn''t for me. I''m currently using a partial to hold my form-focus JavaScript and I pass it the ID of the field I require focus for. #/views/shared/_form_focus.html.erb <script language="javascript" type="text/javascript"><!-- setTimeout( function() { var myfield = document.getElementById(''<%= "#{focusField}" -%>''); myfield.focus(); myfield.select(); } , 1); //--></script> Then I call that in a view (in this example to pass focus to the field with id first_name): <%= render :partial => ''shared/form_focus'', :locals => {:focusField => ''first_name''} -%> For more discussion about this solution, see http://forums.htmlhelp.com/index.php?act=Print&client=printer&f=12&t=5399 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Müller
2008-Dec-05 11:47 UTC
Re: How to set the focus to a specified field in a form
> Does that work for you in Internet Explorer? It didn''t for me. I''m > currently using a partial to hold my form-focus JavaScript and I pass > it the ID of the field I require focus for.Sorry, can''t say for sure. At least not after reading the thread you linked. If I got this right, the problem you mention happens only, if some other javascript (like alert) is executed same time. But I can''t test it right now. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Doyle
2008-Dec-05 13:24 UTC
Re: How to set the focus to a specified field in a form
On Fri, Dec 5, 2008 at 4:50 AM, Thorsten Müller <thorsten-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org>wrote:> > this one requires prototype (for textfield with id "searchtext"): > > <script language="JavaScript"> > $("searchtext").focus(); > </script> >Thank you. That did it (for me with Firefox). --wpd --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hassan Schroeder
2008-Dec-05 17:09 UTC
Re: How to set the focus to a specified field in a form
<script language="JavaScript"> And remember, kids, it''s never too late to pick up bad habits. :-) The "language" attribute of the script tag has been deprecated for, oh, what, like TEN YEARS now? Just sayin'' ... -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Doyle
2008-Dec-05 17:12 UTC
Re: How to set the focus to a specified field in a form
On Fri, Dec 5, 2008 at 12:09 PM, Hassan Schroeder < hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > <script language="JavaScript"> > > And remember, kids, it''s never too late to pick up bad habits. :-) > > The "language" attribute of the script tag has been deprecated for, > oh, what, like TEN YEARS now? > > Just sayin'' ... >Ok, As one of the "kids" to whom you refer (albeit a rather old, hairy "kid"), what would be the good habit I would want use instead? --wpd --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hassan Schroeder
2008-Dec-05 17:41 UTC
Re: How to set the focus to a specified field in a form
2008/12/5 Patrick Doyle <wpdster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> what would be the good habit I would want use instead?Short answer: <script type="text/javascript"> Longer answer: read the spec -- that is, develop the habit of looking up the tags you''re using rather than relying on potentially incorrect or out-of-date online references -- <http://www.w3.org/TR/html401/> Running your pages through the W3C validator will also point out problematic markup, and frequently prevent a lot of styling and JS headaches. FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---