Is there a way to focus core-input or paper-input element?
What I want to achieve is: set cursor to input element so user can start typing.
This way he will not be forced to click on element before writing.
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.
An element can be focused by either using the autofocus="true" attribute or calling the element. focus() method. In both cases, the browser will automatically scroll the element into the viewport.
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.
Input Text focus() MethodThe focus() method is used to give focus to a text field. Tip: Use the blur() method to remove focus from a text field.
core-input has now a .focus()
method it's delegating to the internal 's focus()
From the core-input.html
code:
focus: function() {
this.$.input.focus();
}
What means that in your own code you need to call it like following:
elem[0].focus()
In my own case, I am calling focus
from a timeout
. In this case a bind
is necessary for core-input
to make a right use of this
:
$timeout(elem[0].focus.bind(elem[0]));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With