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 ?
js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)
You can use event.target.id in event handler to get id of element that fired an event.
jQuery attribute methods allows you to manipulate attributes and properties of elements. Use the selector to get the reference of an element(s) and then call jQuery attribute methods to edit it. Important DOM manipulation methods: attr(), prop(), html(), text(), val() etc.
Try this.
example_event : function(event) { //need to get the data-name here var name = $(event.target).data('name'); }
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'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With