Solved

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

Posted on 2010-08-12
2
660 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
extending out dates 4 293
Anyone know where I can access free Ruby on Rails Tutorials? 3 221
What does  =~ mean in Ruby 1 494
ruby gem installation 2 53
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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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