How can i add click event to my el elements in this case multiple li’s. I’m trying to remove li from the dom on click, but the click event is not firing.
App.Views.ListItem = Backbone.View.extend({
el : 'li',
Wrapper : '#list_cntr ul',
template : 'tmplItem',
events : {
'click li' : 'removeMe'
},
initialize : function() {
this.render();
},
render : function() {
$(this.Wrapper).prepend($(this.el).html(App.Templates[this.template](this.model.toJSON())));
},
removeMe : function() {
var eventList = App.Collections.eventCollectionList;
var eid = this.model.get('id');
eventList.remove(eid);
//How to remove the li?
}
});
From the fine manual:
The
elfor your view is the<li>that you want a click handler on so you should be able to drop the selector in yourevents:That will bind
removeMeto clicks on the<li>itself.You’re also setting
el: 'li'when you want to saytagName: 'li'. Theelproperty is your view’s DOM element but'li'won’t do anything useful. If you specify atagNamethen Backbone will create the<li>for you andthis.elwill be the view’s single<li>. This is probably where the strange “it looks like all the<li>s” behavior you’re seeing comes from.Your view should look more like this:
Demo: http://jsfiddle.net/ambiguous/MTh57/