Search code examples
javascripthtmlbuttonpolymerenter

Polymer fire on-tap with enter


I am beginner on Polymer. I think that it is a easy Problem but i could not find out how can i implement this on my application.

I have a button on HTML like:

<paper-button class="button" id="button-wide" raised affirmative on-tap="{{onClick}}">

and in javascript i have function like:

Polymer('dialog', {
        onClick: function() {
            ......
            ......
        }}

And i want fire this button with enter key on keyboard.

Thanks!


Solution

  • Something like this could work:

    Polymer('dialog', {
        onClick: function () {
            // etc..
        },
        onKeyDown: function (e) {
            if (e.which === 13 || e.keyCode === 13) {
                this.onClick();
            }
        },
        attached: function () {
            this.onKeyDown = this.onKeyDown.bind(this);
            document.addEventListener('keydown', this.onKeyDown);
        },
        detached: function () {
            document.removeEventListener('keydown', this.onKeyDown);
        }
    });
    

    where attached and detached are lifecycle methods of the element.