最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - polymer focus() on <paper-input> or <core-input> element - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question asked Jun 30, 2014 at 18:28 wormhitwormhit 3,82740 silver badges46 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 8

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]));

core-input is missing focus()/blur() API, this is essentially a bug.

For now you can do this:

<reference to a core/paper-input>.$.input.focus();

.focusAction(); works in a slightly nicer way, the floating headers will react properly if enabled.

for those using Polymer 1.0 (not OP at time of post)

For the first paper-input you can set focus with this:

        $('paper-input input').first().focus();

you can also select by id for a specific paper-input:

        $('#myValue input').focus();

For Polymer 2.0 use

this.$.id-of-the-Element.focus();

so if your element is like
<paper-input id="myInputElement" type="number" name="myInputElement"> </paper-input>

Then you would do
this.$.myInputElement.focus();

发布评论

评论列表(0)

  1. 暂无评论