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)

Max
  • 1,054
  • 1
  • 12
  • 20
vdegenne
  • 12,272
  • 14
  • 80
  • 106

2 Answers2

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();
mishu
  • 5,347
  • 1
  • 21
  • 39
vdegenne
  • 12,272
  • 14
  • 80
  • 106
  • 4
    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. – Whyser Oct 27 '15 at 21:25
  • by the time I wrote `document.getElementById('my-input')` I used a `` not inside a Polymer Element template, that is why – vdegenne Jun 26 '17 at 23:35
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().

Zikes
  • 5,888
  • 1
  • 30
  • 44
  • 1
    thanks, i didn't mean *autofocus* though, check my schizophrenic answer, that is what I wanted to do. – vdegenne Jun 12 '15 at 14:23