Timmy Von Heiss Timmy Von Heiss - 1 year ago 56
Javascript Question

set mouse focus and move the cursor to the end of the input focus in a Rails js.erb

I have seen this topic, set-mouse-focus-and-move-cursor-to-end-of-input-using-jquery but I cannot get it to work.


def edit
@comment = Comment.find(params[:id])
respond_to do |format|

<%= form_for @comment, url: photo_comment_path(@comment.photo_id, @comment), method: :patch, remote: true do |f| %>
<%= f.text_field :content %>
<%= f.submit class: "hidden" %>
<% end %>

Rails automatically moves
into the
. If I call


this brings the cursor into focus at the beginning of the string. I want it at the end of the string.

I have tried the following and several other combinations but nothing sticks:

var this = $('input#comment_content').val();

Answer Source

I finally got this to work, but it only works one way. You have to add a custom class to the input field and then use that as 'input#comment_content.class'