Given the following simple html:
<div class="someContainer">
<h5>Some other information</h5>
</div>
And the following Backbone view:
var view = Backbone.View.extend({
events: {
'click .someContainer': performAction
},
performAction: function (evt) {
// Do things here
}
});
I find myself doing the following bit of code quite a bit and this seems like a code smell to me. Is there something I am doing wrong or is there a better way to do this?
...performAction: function (evt) {
// Check to see if the evt.target that was clicked is the container and not the h5 (child)
if ($(evt.target).hasClass('someContainer')) {
// Everything is ok, the evt.target is the container
} else {
// the evt.target is NOT the container but the child element so...
var $container = $(evt.target).parent('.someContainer');
// At this point I now have the correct element I am looking for
}
}
This works, obviously but I'm not sure this is good code to write everywhere. I could make a method that I could just call but I'm not sure that actually corrects the code smell, it just outsources it somewhere else.