I'm trying to get started with AJAX and having a problem..

0 views
Skip to first unread message

Bob Smith

unread,
Jul 23, 2010, 6:15:49 PM7/23/10
to Ruby on Rails: Talk
I'm trying to set the focus to the first (and only} field on a search
form and having no luck.
From searching with google, I came up with:

javascript_tag("Event.observe(window, 'load', function() {
var firstForm = $A(document.getElementsByTagName('search'))[0];
Form.focusFirstElement(firstForm);} );")

no response, though. Please help

Bob

Hassan Schroeder

unread,
Jul 23, 2010, 6:32:29 PM7/23/10
to rubyonra...@googlegroups.com
On Fri, Jul 23, 2010 at 3:15 PM, Bob Smith <bsm...@gmail.com> wrote:
> I'm trying to set the focus to the first (and only} field on a search
> form and having no luck.

> javascript_tag("Event.observe(window, 'load', function() {


>  var firstForm = $A(document.getElementsByTagName('search'))[0];

For one thing, there is no HTML tag named "search"; you're probably
looking for an "input" ... :-)

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan

Bob Smith

unread,
Jul 30, 2010, 1:02:19 AM7/30/10
to Ruby on Rails: Talk
Found the answer. I was making it much too complicated..

$("search").focus();

did just fine

Bob

Bob Smith

unread,
Aug 19, 2010, 2:24:56 PM8/19/10
to Ruby on Rails: Talk
or

<%= javascript_tag('$("search").focus();') %>


as part of a view..


Bob

Marnen Laibow-Koser

unread,
Aug 19, 2010, 2:38:27 PM8/19/10
to rubyonra...@googlegroups.com
Bob Smith wrote:
> or
>
> <%= javascript_tag('$("search").focus();') %>
>
>
> as part of a view..
>
>
> Bob

But never do that. JavaScript works best when it's in separate files,
not mixed with HTML.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
mar...@marnen.org

--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages