# How do you give focus to a form field?

**URL:** <https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479>\
**Category:** Rails\
**Created:** [July 23, 2006, 8:54am UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479 "2006-07-23T08:54:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 8:54am UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/1 "2006-07-23T08:54:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 9:21am UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/2 "2006-07-23T09:21:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 9:27am UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/3 "2006-07-23T09:27:50Z")

</div>

> $(‘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…

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 10:51am UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/4 "2006-07-23T10:51:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 3:58pm UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/5 "2006-07-23T15:58:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 23, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/6 "2006-07-23T16:10:51Z")

</div>

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();  
});

---

<div class="post-metadata">

**Author:** ![jeffpritchard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jeffpritchard](https://www.ruby-forum.com/u/jeffpritchard)\
**Post date:** [July 27, 2006, 10:52pm UTC](https://www.ruby-forum.com/t/how-do-you-give-focus-to-a-form-field/99479/7 "2006-07-27T22:52:16Z")

</div>

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
