?
Solved

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

Posted on 2010-08-12
2
Medium Priority
?
666 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

771 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