Solved

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

Posted on 2010-08-12
2
656 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now