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

**URL:** <https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111>\
**Category:** Rails\
**Created:** [July 24, 2010, 12:17am UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111 "2010-07-24T00:17:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 24, 2010, 12:17am UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111/1 "2010-07-24T00:17:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 24, 2010, 12:33am UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111/2 "2010-07-24T00:33:42Z")

</div>

On Fri, Jul 23, 2010 at 3:15 PM, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 19, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111/3 "2010-08-19T20:28:00Z")

</div>

or

\<%= javascript\_tag(’$(“search”).focus();’) %\>

as part of a view…

Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 30, 2010, 7:03am UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111/4 "2010-07-30T07:03:35Z")

</div>

Found the answer. I was making it much too complicated…

$(“search”).focus();

did just fine

Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 19, 2010, 8:38pm UTC](https://www.ruby-forum.com/t/im-trying-to-get-started-with-ajax-and-having-a-problem/192111/5 "2010-08-19T20:38:27Z")

</div>

Bob S. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
