Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7307877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:28:53+00:00 2026-05-28T23:28:53+00:00

I need to bind two events in my backbone.js -view in order to toggle

  • 0

I need to bind two events in my backbone.js-view in order to toggle a menu. The idea is that if a button with the id #toggler is clicked the menu appears and any click outside the #menu element will hide the menu.

Unfortunately I cannot get this working with backbone’s event binding without having the outsideMenuHandler() called on every click regardless if I clicked on the #menu element, or not.

What should I change to make this work?

This is what I have done in backbone.js, which doesn’t work as intended:

myView = Backbone.View.extend({

    events: {
        'click #menu': 'insideMenuHandler',
        'click':       'outsideMenuHandler'
    }

    insideMenuHandler: function(e) {}, // Called when clicking #menu
    outsideMenuHandler: function(e) {} // Called on every click both on and off #menu

}
 

Just as a reference, here’s what I would do using jQuery alone:

$(document).click(function(event) {
    if ( $(event.target).closest('#menu').get(0) == null ) {
        $("#menu").slideUp();
    }
});   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:28:53+00:00Added an answer on May 28, 2026 at 11:28 pm

    There are a couple things you need to sort out.

    First of all, if your insideMenuHandler returns false or calls e.stopPropogation() then your outsideMenuHandler won’t get called for clicks on #menu. For example:

    http://jsfiddle.net/ambiguous/8GjdS/

    But that’s not your whole problem. Your outsideMenuHandler will only be called for clicks on your view; so, if someone clicks on the page outside your view, your outsideMenuHandler won’t get called and your menu won’t go down. If you want the menu to go down when someone clicks anywhere outside #menu, then you’ll have to manually bind to body and manually unbind when your view is destroyed. Something like this:

    var myView = Backbone.View.extend({
        events: {
            'click #menu': 'insideMenuHandler'
        },
    
        initialize: function() {
            _.bindAll(this, 'insideMenuHandler', 'outsideMenuHandler');
        },
    
        render: function() {
            // Both <body> and <html> for paranoia.
            $('body, html').on('click', this.outsideMenuHandler);
            // ...
            return this;
        },
    
        remove: function() {
            // Clean up after ourselves.
            $('body, html').off('click', this.outsideMenuHandler);
            // ...
        },
    
        // ...
        outsideMenuHandler: function(e) {
            // ...
            return false;
        }
    });
    

    and then be sure to properly remove your view. For example:

    http://jsfiddle.net/ambiguous/MgkWG/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a UserControl that contains a menu. I need to bind the Menu.Icon
I have a dropdown. I need to bind with two field of datatable value
I need to bind width property of the GridView column that is created dynamically
I have a set of 'dynamic data' that I need to bind to the
I need to bind two-way a property of a user control to a property
I need to bind an enum to a combobox that is inside a DataGridTemplateColumn,
I have a function that I am calling with two images like so. $('#image1').bind('click',doNext);
I would like to bind two events together. Right now my set up is
I work in Windows Presentation Foundation and I need to bind two buttons together.
On the window I bind two keydown events. The condition for triggering a callback

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.