[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page

It is a memorandum.

What are cursors and carets?

スクリーンショット 2021-01-10 15.31.03.png

Caret ≒ cursor The word caret is used when you want to distinguish it from a mouse cursor.

Main subject

Use autofocus to focus the cursor on the right place. In Ruby, write as follows

autofocus: true

Let's take a look at the case where the item "Nickname" is provided on the new registration screen of Devise. First of all, what will be displayed if autofocus is not used for "Nickname"?

① There is no autofocus in "Nickname"

erb:new.html.erb


<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="field">
    <%= f.label :nickname, "Nickname" %><br />
    <%= f.text_field :nickname %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

(Omitted)

スクリーンショット 2021-01-10 15.39.34.png

In the above case, when I open the new registration web page, the cursor is focused on Email instead of Nickname.

Next, add autofocus to <% = f.text_field: nickname%> and look at Nickname with the cursor in focus.

② "Nickname" has autofocus

erb:new.html.erb


<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="field">
    <%= f.label :nickname, "Nickname" %><br />
    <%= f.text_field :nickname, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>

(Omitted)

スクリーンショット 2021-01-10 15.47.50.png

By giving autofocus to "Nickname" in this way, the place where the cursor is automatically placed first becomes Nickname.

Reference article

https://www.sophia-it.com/content/%E3%82%AD%E3%83%A3%E3%83%AC%E3%83%83%E3%83%88 http://www.htmq.com/html5/input_autofocus.shtml

Recommended Posts

[Ruby On Rails / HTML] How to display the cursor (caret) at an appropriate place when displaying a web page
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby on Rails] How to display error messages
How to display a web page in Java
[Rails] When transitioning to a page with link_to, move to the specified location on the page
[Ruby on Rails] How to change the column name
[Rails] How to display an image in the view
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
[Ruby on Rails] Quickly display the page title in the browser
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
How to make an application with ruby on rails (assuming that the environment has been built)
How to use Ruby on Rails
[Rails] How to put a crown mark on the ranking function
How to create a web server on an EC2 instance on AWS
(Ruby on Rails6) Create a function to edit the posted content
[Rails 5] How to display the password change screen when using devise
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Swing] How to display an arbitrary name on the menu bar on Mac
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to solve the local environment construction of Ruby on Rails (MAC)!
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
How to add / remove Ruby on Rails columns
When the Ruby on Rails terminal rolls back
Determine the current page with Ruby on Rails
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
When I tried to reproduce Progate's Rails app on my PC (local environment), I got stuck at the image display
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Ruby on Rails] How to stop when Rails server cannot be stopped by Ctrl + C
How to install Ruby on an EC2 instance on AWS
[Introduction] Try to create a Ruby on Rails application
How to set the display time to Japan time in Rails
[Ruby on Rails] How to write enum in Japanese
Rails / Ruby: How to get HTML text for Mail
How to make an almost static page with rails
How to make JavaScript work on a specific page
[Ruby On Rails] How to reset DB in Heroku
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby on Rails] I get a warning when executing RSpec due to a different gem version.
How to reduce the load on the program even a little when combining characters with JAVA
How to change a string in an array to a number in Ruby
(Ruby on Rails6) Display of the database that got the id of the database
How to retrieve the hash value in an array in Ruby
From Ruby on Rails error message display to Japanese localization
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
How to specify db when creating an app with rails