Using a Backbone.js View, say I want to include the following events:
events: {
'click a': 'link',
'click': 'openPanel'
}
How can I avoid openPanel to be fired when I click on a link. What I want is to have a clickable box which will trigger an action, but this box can have elements which should trigger other actions, and not the parent action. Think for example Twitter.com, and links in Tweets/right hand panel.
Return "false" in your "link" function.
I've been using
e.stopImmediatePropagation();
in order to keep the event from propagating. I wish there was a shorter way to do this. I would like return false; but that is due to my familiarity with jQueryEach of your event handlers will be passed an event object when it's triggered. Inside your handler, you need to leverage jQuery's event.stopPropagation() method. For example:
Two other methods that might work for you:
1
Then openPanel will not capture
click
events on any<a>
or child of an<a>
(in case you have an icon in your<a>
tag).2
At the top of the
openPanel
method, make sure the event target wasn't an<a>
:Note that both of these methods still allow the
openPanel
function to be called from elsewhere (from a parent view or another function on this view, for example). Just don't pass anevent
argument and it'll be fine. You also don't have to do anything special in yourlink
function -- just handle the click event and move on. Although you'll probably still want to callevent.preventDefault()
.The JQuery
preventDefault
method would also be a good option.