Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

give focus to first input field or textarea on page (See related posts)

Place the following in your application.js file and make sure your layout includes "javascript_include_tag :defaults".
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' || e.tagName.toUpperCase() == 'SELECT';
  });
  if (e) e.focus();
});

Comments on this post

brianzimmer posts on Jun 29, 2006 at 16:34
Thanks! Works great!
moneypenny posts on Aug 31, 2006 at 19:45
Very nice; thanks!

If you could make it work for select menus now, too... ;)
remvee posts on Sep 01, 2006 at 11:27
Select included. thanx.

You need to create an account or log in to post comments to this site.


Click here to browse all 4863 code snippets

Related Posts