How do you give focus to a form field?

When you put up a page with a form on it and the top item is a text
field it would be nice to have that field already focused instead of
forcing the user to click in it before they start typing. Is there a
rails (i.e. probably Prototype) way of giving focus to a field? I did
some searching and found nothing.

thanks,
jp

P.S. I’m on a Mac. Perhaps a winoze and/or unix automatically gives
focus to the first text field in a form?

Jeff P. wrote:

Is there a
rails (i.e. probably Prototype) way of giving focus to a field? I did
some searching and found nothing.

thanks,
jp

P.S. I’m on a Mac. Perhaps a winoze and/or unix automatically gives
focus to the first text field in a form?

It’s a javascript thing, prototype makes it shorter though:

$(‘id_of_item_to_focus’).focus();

Win/linux don’t do this automatically.

phil

$(‘id_of_item_to_focus’).focus();

Win/linux don’t do this automatically.

what are you saying? safari auto-focuses the first text field and
firefox doesnt? a line of greasemoneky can solve that…

carmen wrote:

$(‘id_of_item_to_focus’).focus();

Win/linux don’t do this automatically.

what are you saying? safari auto-focuses the first text field and firefox doesnt? a line of greasemoneky can solve that…

Not sure I understand what you are saying…

the OP said:

P.S. I’m on a Mac. Perhaps a winoze and/or unix automatically gives
focus to the first text field in a form?

I was just saying you can’t count on that on the other platforms either.
That’s mostly FF and IE, which don’t auto-focus (at least not out of the
box).

cheers,
phil

Phillip K. wrote:

carmen wrote:

the OP said:

P.S. I’m on a Mac. Perhaps a winoze and/or unix automatically gives
focus to the first text field in a form?

I was just saying you can’t count on that on the other platforms either.
That’s mostly FF and IE, which don’t auto-focus (at least not out of the
box).

cheers,
phil

Thanks guys. Sorry if I worded it poorly. I run a mac and Safari does
not do this automagically. I didn’t take the time to start up Parallels
and see if Windoze does it or not.

I’ll try the one liner you suggested and see if it does the trick.

thanks,
jp

you could put this in your application.js

//focuses on the first element on a page
Event.observe(window, ‘load’, function() {
var e = $A(document.getElementsByTagName(’*’)).find(function(e) {
return (e.tagName.toUpperCase() == ‘INPUT’ && (e.type == ‘text’ ||
e.type == ‘password’))
|| e.tagName.toUpperCase() == ‘TEXTAREA’;
});
if (e) e.focus();
});

Phillip K. wrote:

It’s a javascript thing, prototype makes it shorter though:

$(‘id_of_item_to_focus’).focus();

Win/linux don’t do this automatically.

phil

Where do I put that code? Here’s the form I want to have autofocused:

<%= text_field ‘menu_item’, ‘name’ %>

Do I put it in the view page?

I’d appreciate your help. Thanks.

Chris

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs