I have a View in Backbone that contains an inner div. I want to hide the div when the user clicks outside of the div.
I’m not sure how to set up an Event inside of the View that says “click NOT #inner_div”: “removeDiv”.
Any suggestions on how to do this?
The usual approach is to attach a click handler directly to
<body>and then have that close or hide your<div>. For example:If you just want to hide the
<div>rather than remove it, just bind clicks on<body>to a different method thanremove; you’ll still want to remove the click handler from<body>in yourremovethough. Also, you’ll want to trap click events on your view’selto keep them from getting to<body>.Demo: http://jsfiddle.net/ambiguous/R698h/
If you have other elements that care about click events then you could absolutely position a
<div>to cover up the<body>and then bind your click handler to that. You could have a look at the jQuery BlockUI plugin to see how this is done.