Reputation: 42350
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
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
Reputation: 69905
Try this.
example_event : function(event) {
//need to get the data-name here
var name = $(event.target).data('name');
}
Upvotes: 52