Solved

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

Posted on 2010-08-12
2
665 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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