Reputation: 5758
I'm having a bit of a problem. I have several a
tags. I would like to assign a data
attribute to each and reference the value of the data
attribute using JQuery
. I would then like to set the value of a hidden field in my form to this data
attribute.
However, I am unsure of how to do this. Currently I have this link.
<a href="" class="myclass" name="hello">Say Hello</a>
In my JQuery
I would like to be able to get the name
value....
Something like this ....
$(".myclass").click(function (e) {
e.preventDefault();
console.log(e.attr('name'));
});
and output the value hello
Upvotes: 0
Views: 44
Reputation: 93561
Inside a click handler this
is the DOM element clicked.
Use $(this)
to convert it to a jQuery object and use the attr
on that:
$(".myclass").click(function (e) {
e.preventDefault();
console.log($(this).attr('name'));
});
Upvotes: 4