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

  • Home
  • SEARCH
  • 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 8205509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:16:06+00:00 2026-06-07T08:16:06+00:00

Noob here learning backbone.js. I was trying to create a simple horizontal menu (each

  • 0

Noob here learning backbone.js. I was trying to create a simple horizontal menu (each menu item is a model and the whole menu is a collection). I would like to know which menu item was clicked. In menuItemView, I bind the “click a” event to a “clicked” function, but it does not fire when I click. I closest solution to this I found was at: http://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/ and I tried to replicate similar functionality … even though I think I have copied it almost same, I still can’t seem to get it to work. I did go through all the posts that seem to address the issue of detecting which model/item got clicked, but none of them seemed to help. Any help would be appreciated. I know that a clickable menu and getting its id is probably a lot more straightforward in simple jquery, but I thought I would use this as an example to learn backbone.

HTML

<header>
<ul id="nav">
</ul>
</header>

JAVASCRIPT

(function($) {

window.app = window.app || {};

//Goal:if selected=true, menu item should be highlighted
MenuItem = Backbone.Model.extend({
    label: "Default Label",
    selected: false,
    id: 0
});

MenuList = Backbone.Collection.extend({
    model: MenuItem,
    initialize: function(models, options) {
        //nothing ... yet
    }
});


//View for a single item. Returns el that looks like:
//  <li id='4' class='false'><a href='#4'> Item 4 </a> </li?
MenuItemView = Backbone.View.extend({
    tagName: "li",

    render: function() {
        var id  = this.model.get("id");
        var cls = this.model.get("selected");
        var lbl = this.model.get("label");
        $(this.el).attr('id', id).addClass(cls.toString());
        $(this.el).html("<a href=#" + id + " >" + lbl + "</a> </li>");
        return this; //recommended as this enables calls to be chained.
    },

    events: {
        "click a": "clicked" //Firebug shows 'a' element bound to
                             //native 'click' and not 'clicked'. WHY?
    },

    clicked: function(ev){
        alert($(ev.target).text()); //NOT HAPPENING :-(
        //do something to highlight menu item via css stuff.
    }
});

MenuListView = Backbone.View.extend({
    el: $("header > ul"),

    initialize: function() {
        this.menulist = new MenuList(null, { view: this });
        _.bindAll(this, "renderItem");
    },

    renderItem: function(model) {
        var menuitemView = new MenuItemView({ model: model });
        menuitemView.render();
        $(this.el).append(menuitemView.el);
    },

    render: function() {
        this.collection.each(this.renderItem);
    },

    setActivePage: function(ev) {
        alert($(ev.target).text());
        window.app.footerview.updatePageNumber(10);
    }
});


var items = new MenuList([
{id: 1, label: "item 1", selected: true},
{id: 2,label: "item 2",selected: false},
{id: 3,label: "item 3",selected: false},
{id: 4,label: "item 4",selected: false}
]);


window.app.menuview = new MenuListView({ collection: items });
window.app.menuview.render();
$("header").html(window.app.menuview.el);

})(jQuery);

The running example for this can be seen at jsfiddle: .. see http://jsfiddle.net/gopal_a/4uzcb/3/

  • 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-06-07T08:16:08+00:00Added an answer on June 7, 2026 at 8:16 am

    Your problem is right here:

    $("header").html(window.app.menuview.el);
    

    Your window.app.menuview.el is already in the DOM since you’ve defined it like this:

    el: $("header > ul")
    

    When you $('header').html(...), you’re clearing out what’s already in <header> and then replacing it with your el; but, your .html() call kills the event delegators that Backbone installs to handle events on the views. The result is that you end up with the right HTML in the DOM but you don’t have any event handling attached to them.

    Drop your $("header").html(window.app.menuview.el) and things will start to work.

    As an aside, you don’t have to say $(this.el), Backbone views already have this.$el defined so you can use this.$el instead.

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

Sidebar

Related Questions

Noob here trying to figure out the simple task of adding a page to
noob here still experimenting with templates. Trying to write a message processing class template
Total noob here with javascript. I'm trying to alter a function. This is the
Noob here - I'm working on a simple Javascript calculator, and I'm using lists
Doctrine NOOB here, trying to figure out how to prevent a duplicate record in
noob here, I'm trying to transfer a file using rsync from windows to linux.
XNA noob here, learning every day. I just worked out how to composite multiple
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
Total, embarassing noob here, so here goes nothing: if you wanted to create a
Total noob here learning openGL and I don't have any code to post because

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.