How to focus a paper-input with Polymer 1.0?
Asked Answered
I

2

10

This question is a follow-up to this one: polymer focus() on <paper-input> or <core-input> element

How can I focus in a paper-input element using the Javascript API ?

(using Polymer 1.0)

Impend answered 12/6, 2015 at 14:1 Comment(0)
I
12

If you have an element:

<paper-input id="my-input" label="What's on your mind?"></paper-input>

paper-input is a wrapper for business logic and stylish of a more deep-down input element which you can reach through:

document.getElementById('my-input').$.input

To focus, just write:

document.getElementById('my-input').$.input.focus();
Impend answered 12/6, 2015 at 14:21 Comment(2)
Thanks, was looking for this! You should however for the sake of prettiness use this.$.my-input.$.input.focus(). Also, if you happen to do this the same time as the paper-input is being shown, you should call focus() inside a setTimeout(function(){...}, 0} call.Domitiladomonic
by the time I wrote document.getElementById('my-input') I used a <paper-input> not inside a Polymer Element template, that is whyImpend
C
10

<paper-input autofocus></paper-input> will automatically focus immediately, or if you'd prefer to control the timing yourself in Javascript you can use paperInput.$.input.focus().

Coma answered 12/6, 2015 at 14:19 Comment(1)
thanks, i didn't mean autofocus though, check my schizophrenic answer, that is what I wanted to do.Impend

© 2022 - 2024 — McMap. All rights reserved.