javascriptbackbone.jsbackbone-events

In a backbonejs view, do keypress/keyup/keydown events work only with input and not other elements?


What would be the best way to detect onkeypress event on a div in backbonejs view? Currently putting it in the events is not working,

events: {
 "keypress div#xyz": "myFunction" 
}

Solution

  • The problem is that keyboard events are sent only to the element that has focus (for example a form input) and are not bubbled to container elements. http://api.jquery.com/keyup/

    You should bind it to the exact element(s) in which text can be entered.

    events: { "keypress #xyz input" : "myFunction" }