Erb and js question

Hi All,

I did some looking around on google and found a fix for setting the
default location of the cursor on a page. The fix is mentioned here:

http://techsupt.winbatch.com/TS/T000002021F19.html

So my goal is to use Javascript, and place the the focus on one of the
boxes. The focus would mean that the typing cursor would be in a certain
box, but not the mouse cursor.

Here is my code:

cat login.html.erb

<% form_tag :action => 'login' do %>
Username: <%= text_field_tag 'username' %> Password: <%= password_field_tag 'password' %> <%= image_submit_tag 'login.png' %>

<% end %>

Yet I am still unsure how to integrate this code, maybe just the JS
block?

thank you!

This looks like a JavaScript question. I recommend this forum :

http://w3schools.invisionzone.com/

I suggest you need the focus () function called by the onload event.

Derek S. wrote:

Here is my code:

cat login.html.erb

<% form_tag :action => 'login' do %>
Username: <%= text_field_tag 'username' %> Password: <%= password_field_tag 'password' %> <%= image_submit_tag 'login.png' %>

<% end %>

Yet I am still unsure how to integrate this code, maybe just the JS
block?

This isn’t really a Ruby question, but briefly: you can emit a

which does what you want. However:

  1. You want this to happen when the page has finished loading, so write
    it to be triggered on the onload event.

  2. It’s cleaner to put it in the of your document, which could be
    in the layout or using content_for :head

  3. It’s cleaner to put it in a separate Javascript file, e.g.
    public/javascripts/focus_user.js, and then all you need is:

<%= javascript_include_tag ‘focus_user’ %>

This sort of stuff is wonderfully easy to do using jQuery, and I
recommend it strongly. All you would need is:

jQuery( function($) {
$(’#username’).focus();
});

Derek S. wrote:

Yeah I heard of jquery from my rails beginner class and now I am trying
it, however it is still not working. Please review my code.

jQuery problems don’t really belong on a Ruby mailing list.

I don’t see anything wrong with what you have, except I usually write

rather than I suggest you debug this using firefox plus firebug. This will let you open a javascript console, see if any errors are reported, then issue the $('#username')... javascript line interactively.

This sort of stuff is wonderfully easy to do using jQuery, and I
recommend it strongly. All you would need is:

jQuery( function($) {
$(’#username’).focus();
});

Yeah I heard of jquery from my rails beginner class and now I am trying
it, however it is still not working. Please review my code.

CODE

<% form_tag :action => 'login' do %>
Username: <%= text_field_tag 'username' %> Password: <%= password_field_tag 'password' %> <%= image_submit_tag 'login.png' %>

<% end %>

Iñigo Medina wrote:

There is no id defined with “username” in your form.

I think you’ll find there is; the code has

<%= text_field_tag ‘username’ %>

and the Rails helper adds an id for you. From the action_view
documentation:

  # ==== Examples
  #   text_field_tag 'name'
  #   # => <input id="name" name="name" type="text" />

There is no id defined with “username” in your form. Why don’t you try
to
get first element of your form and then focus it?

iñ

Derek S. wrote:

Erm, I missed the obvious thing there, which is that you’re not loading
jquery properly.

If your browser is loading the page directly from disk into your browser
with a file:// URL, then you’d want

src=“file:///usr/local/vrep/test/public/javascripts/jquery-1.3.2.min.js”

If you’re serving the page from a webserver, then you’d just want

src="/javascripts/jquery-1.3.2.min.js"

There is a Rails helper you can use instead:

<%= javascript_include_tag “jquery-1.3.2.min.js” %>

This also does some clever stuff appending the timestamp of the file to
the URL, so if you change the file on disk it forces the browser to
fetch the new version.

The best practice would be for this script to live in the of your
document. Note that jQuery( …function… ) sets up an onLoad handler
which calls your function only when the entire page is loaded, so it’s
OK to set this early because it won’t be run until later.

I think you’ll find there is; the code has

<%= text_field_tag ‘username’ %>

Yep, sorry. I forgot the way rails works with ids.
Thanks.

iñigo

On Sun, Nov 29, 2009 at 12:52 AM, Brian C. [email protected]
wrote:

I don’t see anything wrong with what you have, except I usually write

rather than

Even though the “language” attribute of the script tag has been
deprecated since 1999??

Hassan S. wrote:

On Sun, Nov 29, 2009 at 12:52 AM, Brian C. [email protected]
wrote:

I don’t see anything wrong with what you have, except I usually write
�
rather than
�

Even though the “language” attribute of the script tag has been
deprecated since 1999??

Hmm. I picked up this practice from “Javascript - The Definitive Guide”
(5th edition, August 2006), but it turns out I misread it:

‘At the time of this writing, “application/javascript” is not well
supported, however’

But what I should have used instead is type=“text/javascript”, rather
than language=“javascript”

Thanks for pointing this out.