Search code examples
polymerpaper-elements

Polymer input capture enter event


I'm trying to detect when a user presses return/enter in a chat box to send the message. How do I detect this for a paper-input element?


Solution

  • paper-input inherits from core-input, which fires a change event when the users hits enter/return or the element loses focus. If you don't care about the losing focus case (e.g only want the case where the user hits ENTER), you could check the document.activeElement:

    document.querySelector('paper-input').addEventListener('change', function(e) {
      if (document.activeElement == this) {
        console.log('ENTER hit on aper-input');
      }
    });
    

    http://jsbin.com/godaqugacecu/1/edit

    See http://www.polymer-project.org/docs/elements/core-elements.html#core-input.