Javascript Autofocus in Form

By Jimmy Bonney | October 3, 2011

Code

When using javascript in Rails in order to fill in forms and remain on the same page, it is nice if after submitting and resetting the form, the focus is set back on the first element of the form. In order to do this, you can simply use the following piece of JS in your file.js.erb:

1
$("input:text:visible:first").focus();

For the time being, comments are managed by Disqus, a third-party library. I will eventually replace it with another solution, but the timeline is unclear. Considering the amount of data being loaded, if you would like to view comments or post a comment, click on the button below. For more information about why you see this button, take a look at the following article.