Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give focus property to text box in Ruby on Rails?

I have a problem with setting the focus to the textbox for a login page in Ruby on Rails. I want to give focus to a user text field. Is there any way to give focus property to text_field?

like image 275
Arpit Vaishnav Avatar asked Jul 15 '10 11:07

Arpit Vaishnav


People also ask

How do you set focus on textfield?

Just select an element (maybe the button, panel or the window) and use the "Code"-tab in the "Properties"-dialog.

How to set focus on textbox in HTML?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.

How to focus field in JavaScript?

JavaScript | Focus() JavaScript focus method is used to give focus to a html element. It sets the element as the active element in the current document. It can be applied to one html element at a single time in a current document. The element can either be a button or a text field or a window etc.


1 Answers

In HTML5 you can do something like this:

<%= f.text_field :my_field, :autofocus => true %> 

The only way to achieve this without using HTML 5 is with JavaScript if I'm not mistaken. This has nothing to do with Rails, it's an HTML attribute.

like image 169
Cimm Avatar answered Oct 04 '22 16:10

Cimm