Solved

How do I focus cursor on a specific text_field_tag when loading a partial with AJAX?

Posted on 2010-08-12
2
662 Views
Last Modified: 2013-11-13
I have found solutions for focusing on the text_field when the entire page is loaded, however I have a partial that includes a text_field_tag with an associated observe_field.  My problem is that when the partial is reloaded every half second, the focus does not go back to the text_field_tag, which is annoying since the user may not be done typing their filter text.  I have tried putting the text_field_tag outside of the partial, and that keeps the focus there, but screws up other things (I have the ability to reorder based on columns in a table, and it doesn't seem to remember the order preferences the use chose).
0
Comment
Question by:jef9c
2 Comments
 
LVL 12

Accepted Solution

by:
JESii earned 500 total points
ID: 33424769
I set up an application helper:

  def set_focus(id)
    javascript_tag("$(\"#{id}\").focus()")
  end

and then do this:
 
<%= set_focus "user_login" %

in my view.
0
 

Author Comment

by:jef9c
ID: 33433603
Cool, that was a major improvement.  One problem I still had was that it would put the cursor at the beginning of the text_field.  I found a solution to that problem, which is to add an :onfocus parameter.

<%= text_field_tag 'search', @search, :onfocus => "this.value = this.value" %>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question