14

Here is my basic backbone view for changing routes. I would like to get the href attribute of the clicked link. How to do that? Here is a code bellow:

var Menu = Backbone.View.extend({   
        el: '.nav', 
        events: {
            'click a' : 'changeRoute'
        },  
        changeRoute: function(e) {
            e.preventDefault();
            //var href = $(this).attr("href");
            router.navigate(href, true);
        }
 });

I am a newbie in backbone, so please have mercy :)

hjuster
  • 3,985
  • 9
  • 34
  • 51
  • Same issue: http://stackoverflow.com/questions/13807243/how-receive-link-attributes-of-event-in-backbone/13807330#13807330 – WiredPrairie Apr 02 '13 at 14:11

1 Answers1

33

you can use: var element = $(e.currentTarget);

then any attributes can be called like this: element.attr('id')

so in your code above:

changeRoute: function(e) {
   e.preventDefault();
   var href = $(e.currentTarget).attr("href");
   router.navigate(href, true);
}
Alex Curtis
  • 5,659
  • 3
  • 28
  • 31