GSto
GSto

Reputation: 42350

How to get the jQuery element (or attributes) in a click event in Backbone.js?

I have the following code:

HTML:

<div id='example'>
 <a href='#' data-name='foo' class='example-link'>Click Me</a>
 <a href='#' data-name='bar' class='example-link'>Click Me</a>
</div>

JavaScript

example_view = Backbone.View.extend({
    el: $("#example"),
    events: {
      'click .example-link' : 'example_event'
    },
    example_event : function(event) {
      //need to get the data-name here
    } 
});

how can I get the data-name attribute of the link that was clicked inside of the example_event function ?

Upvotes: 25

Views: 35457

Answers (2)

tomsabin
tomsabin

Reputation: 265

You can also do this without jQuery using JavaScript's getAttribute method:

example_event : function(event) {
  //need to get the data-name here
  var name = event.target.getAttribute('data-name');
} 

Upvotes: 21

ShankarSangoli
ShankarSangoli

Reputation: 69905

Try this.

example_event : function(event) {
  //need to get the data-name here
  var name = $(event.target).data('name');
} 

Upvotes: 52

Related Questions