Reputation: 2546
Is there a way I can execute my method when someone finishes adding/editing data on an input text and presses enter or tab in Backbone?
Upvotes: 3
Views: 5516
Reputation: 3684
Here is an example of how to implement an event upon hitting return in a text field:
var SearchView = Marionette.ItemView.extend({
template: "#template",
events: {
'keypress #search-input' : 'searchKeywords',
},
searchKeywords: function(e){
if ( e.which === 13 ) {
var keywords = $(e.target).val();
if(keywords === '') return;
this.model.set({keywords: keywords});
}
}
});
The event.which property normalizes event.keyCode and event.charCode.
http://api.jquery.com/event.which/
Upvotes: 8
Reputation: 2752
You can listen to keyup
, keypress
events and compare keyCode
property of the event to 13
.
When user navigates form fields by pressing tab key the events are focus
, focusin
and focusout
Binding to these events should be created as usual with events
dictionary.
Backbone uses same events as jQuery see Form Events for more info.
Upvotes: 2