I have a name
attribute assigned to a hyperlink.
When I do the following with jQuery link_name
does not return anything.
Am I doing something wrong?
$("body").delegate("a", "click", function (event) {
var link_name = $(this).attr('name');
alert(link_name);
jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.
To get the name, you'd use $(selector). attr('name') which would return (in your example) 'xxxxx' .
Using jQuery The idea is to use the . attr() method, which returns the attribute's value for an element if it is present and returns undefined if the attribute doesn't exist.
I'd use this (using newest jQuery):
$("body").on("click", "a", function (event) {
var link_name = $(this).attr('name');
alert(link_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